共用方式為


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;

另請參閱