Looking at 'System.IO.File.AppendAllText' code from the GAC it calls another method called 'InternalAppendAllText' which creates a new StreamWriter and writes the content to the file.
//mscorlib, System.IO
private static void InternalAppendAllText(string path, string contents, Encoding encoding)
{
using (StreamWriter writer = new StreamWriter(path, true, encoding))
{
writer.Write(contents);
}
}
My question is if I did a for loop calling System.IO.AppentAllText 5 times for example will the StreamWriter be created 5 times, each iteration it is initialized and disposed or it is only initialized once?
Example:
for(int i = 1; i < 4; ++i)
{
System.IO.File.AppendAllText("a", "a");
}
a. (It doesn't need to be an absolute filename.)forloop with the++itrick is a good way of making off-by-one mistakes, especially if your code will be used by others. It's an extremely convoluted and unclear way of looping 5 times.