どちらがうまくいくのですか? StreamWriter
クラスからオブジェクトを作成し、メソッドで頻繁に使用して最後に処分する方が良いですか?または、StringBuilder
のオブジェクトを使用し、StreamWriter
からオブジェクトを作成してすぐに処分する方が良いでしょうか?Streamwriter vs StringBuilder
1)
var Write = new StreamWriter(string.Format("{0}{1}{2}", Environment.CurrentDirectory, Path.DirectorySeparatorChar, "Dummy.txt"));
for (int i = 0; i < 100; i++)
{
//Do something include calculation
Write.WriteLine(something);
}
Write.Flush();
Write.Dispose();
2)
var Str = new StringBuilder();
for (int i = 0; i < 100; i++)
{
//Do something include calculation
Str.AppendLine(something);
}
var Write = new StreamWriter(string.Format("{0}{1}{2}", Environment.CurrentDirectory, Path.DirectorySeparatorChar, "Dummy.txt"));
Write.Write(Str);
Write.Flush();
Write.Dispose();
ドン」 'Path.Combine(Environment.CurrentDirectory、" Dummy.txt ")'を使用してパス上で 'string.Format'を使用しないでください。 – Alxandr
すぐに 'Disposal'のために' using(){} '構造を使う方法を研究しよう – MethodMan
streamWritersで 'using'ブロックを使う:http://stackoverflow.com/questions/212198/what-is-the-c-シャープブロックを使用する理由と私はそれを使用する必要がありますそれは – 5uperdan