C#模拟D的区间.

using System;

#nullable enable

// ensure this is a stack only struct
ref struct ScopeExit {
    public ScopeExit(Action cleaner)
    {
        this.cleaner = cleaner;
    }

    public void Dispose()
    {
      try
      {
         cleaner();
      }
      catch {
         // maybe do something interesting here
      }
    }

    private Action cleaner;

}

public class Example {
    public static void Main() {
        using var netSocket = new ScopeExit(() => { /* clean somethig */});
    }
}
posted @ 2021-10-09 17:25  zjh6  阅读(22)  评论(0)    收藏  举报  来源