开发者

Difference between using StreamWriter constructor and File.CreateText

What's the difference (CPU usage, MSIL, etc) between:

StreamWriter sw = new StreamWriter("C:\test.txt");
开发者_Python百科

and:

StreamWriter sw = File.CreateText("C:\test.txt");

?


Not much... (via Reflector)

[SecuritySafeCritical]
public static StreamWriter CreateText(string path)
{
    if (path == null)
    {
        throw new ArgumentNullException("path");
    }
    return new StreamWriter(path, false);  // append=false is the default anyway
}

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜