使用 StreamWriter 构造函数和 File.CreateText 之间的区别
之间有什么区别(CPU 使用率、MSIL 等)
StreamWriter sw = new StreamWriter("C:\test.txt");
:和:
StreamWriter sw = File.CreateText("C:\test.txt");
?
What's the difference (CPU usage, MSIL, etc) between:
StreamWriter sw = new StreamWriter("C:\test.txt");
and:
StreamWriter sw = File.CreateText("C:\test.txt");
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不多...(来自Reflector)
尽管我更喜欢使用文件,但它的价值是什么.* 工厂方法,因为我认为它们看起来更干净,并且比将一堆构造函数参数传递给 Stream 或 StreamWriter 更具可读性,因为如果您不查看定义,则很难记住哪些重载会做什么。
此外,JIT 编译几乎肯定会内联调用,因此即使单个附加方法调用的微小开销也可能不会产生。
Not much... (via Reflector)
For what it's worth though I prefer using File.* factory methods because I think they look cleaner and are more readable than passing a bunch of constructor parameters to Stream or StreamWriter because it's hard to remember which overloads do what if you're not looking at the definition.
Also, JIT compilation will almost certainly inline the call anyway so even the miniscule overhead of a single additional method call will likely not be incurred.