using(C# 参考)
using
关键字有两个主要用途:
using
语句定义一个范围,在此范围的末尾将释放对象:
string filePath = "example.txt";
string textToWrite = "Hello, this is a test message!";
// Use the using statement to ensure the StreamWriter is properly disposed of
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.WriteLine(textToWrite);
}
using
指令为命名空间创建别名,或导入在其他命名空间中定义的类型:
using System;
using System.IO;