首页 > 代码库 > IDisposable最佳实现模式

IDisposable最佳实现模式

代码库

/// <summary>
/// 实现了IDisposable接口的类。
/// </summary>
public class ConnectionManager : IDisposable
{
    #region IDisposable模式

    /// <summary>
    /// 析构函数。
    /// </summary>
    ~ConnectionManager()
    {
        Dispose(false);
    }

    /// <summary>
    /// 真正的释放资源函数。
    /// </summary>
    /// <param name="disposing"></param>
    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            // Clean up all managed resources that is finalizable
            // ...
        }
        // Clean up all native resources
        // ...
    }

    /// <summary>
    /// 提供使用者一个显式释放资源的方法。
    /// </summary>
    public void Dispose()
    {
        Dispose(true);
        // Tell the GC not to execute the deconstructor any more
        GC.SuppressFinalize(this);
    }
}

—— Kevin Yang

本博客遵循CC协议2.5,即署名-非商业性使用-相同方式共享
写作很辛苦,转载请注明作者以及原文链接~
如果你喜欢我的文章,你可以订阅我的博客:-D点击订阅我的文章

  1. X﹏X 到现在还没有评论~
  1. 暂时没有trackbacks.