可能重複:
What is the C# Using block and why should I use it?正在使用Better?
我的問題:使用using(a){do something with a}
只是聲明瞭 'A',並使用它的方式更好。即:更安全,更快,...
查看示例澄清。
實施例1:(不使用)
StreamWriter sw;
string line;
sw = new StreamWriter("D:\\NewCon.xml");
sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
sw.WriteLine("<config>");
for (int i = 0; i >=36; i++)
{
line = "";
line = "<" + xmlnodes[i] + ">";
line += vals[i];
line += "</" + xmlnodes[i] + ">";
sw.WriteLine(line);
}
sw.WriteLine("</config>");
sw.Close();
sw.Dispose();
實施例2:(使用)
string line;
using (sw = new StreamWriter("D:\\NewCon.xml"))
{
sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
sw.WriteLine("<config>");
for (int i = 0; i >= 36; i++)
{
line = "";
line = "<" + xmlnodes[i] + ">";
line += vals[i];
line += "</" + xmlnodes[i] + ">";
sw.WriteLine(line);
}
sw.WriteLine("</config>");
}
偉大的答案謝謝你爲我清理! – 2011-01-06 14:26:14