The difference between using the StreamWriter constructor and File.CreateText

What is the difference (CPU usage, MSIL, etc.) between:

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

and

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

?

+4
source share
1 answer

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 } 

What is it worth, although I prefer to use File methods. * factory, because I think they look cleaner and more readable than passing a lot of parameters to the Stream or StreamWriter constructor, because it's hard to remember what overloads do if you are "not looking at the definition."

In addition, compiling a JIT will almost certainly embed the call anyway, so even the slightest overhead of one extra method call will most likely not be incurred.

+6
source

Source: https://habr.com/ru/post/1316482/


All Articles