What's the difference (CPU usage, MSIL, etc) between:
StreamWriter sw = new StreamWriter("C:\test.txt");
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With