'using' C# 中局部变量声明前的关键字?

'using' keyword before local variable declaration in C#?

您好,我正在研究 C# 中的一些示例并遇到了一些类似于以下的代码:

private void WriteData(string filePath)
{
    using var writer = new System.IO.StreamWriter(filePath);

    //Logic to write data here
}

我想知道using在变量声明中有什么用处和意义。 还有它与简单变量声明有何不同:

var writer = new System.IO.StreamWriter(filePath);

它是传统 using 语句的 C# 8 语法糖,可确保为实现 IDisposable 接口的类型调用 Dispose() 方法。

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement#example