어느 쪽이 더 잘 작동 하나 정확합니까? 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
즉시'폐기 '를 위해'using() {}'구조체를 사용하는 방법을 연구하려고 시도합니다 – MethodMan
streamWriters에서'using '블록을 사용하십시오 : http://stackoverflow.com/questions/212198/what-is-the-c- 날카로운 사용 - 블록 - 그리고 왜 - 사용해야 - 그것 - – 5uperdan