C#ref struct要怎么实现IDisposable

众所周知 ref struct 因为必须在栈上且不能被装箱,所以不能实现接口,但是如果你的 ref struct 中有一个 void Dispose() 那么就可以用 using 语法实现对象的自动销毁。.

ref struct MyDisposable
{
    public void Dispose() => throw new NotImplementedException();
}

class Program
{
    public static void Main()
    {
        using var y = new MyDisposable();
        // ......
    }
}