且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

正确实施 IDisposable

更新时间:2023-01-10 09:54:30

这将是正确的实现,尽管我在您发布的代码中看不到您需要处理的任何内容.您只需要在以下情况下实现 IDisposable:

This would be the correct implementation, although I don't see anything you need to dispose in the code you posted. You only need to implement IDisposable when:

  1. 您有非托管资源
  2. 你坚持引用那些本身就是一次性的东西.

您发布的代码中的任何内容都不需要处理.

Nothing in the code you posted needs to be disposed.

public class User : IDisposable
{
    public int id { get; protected set; }
    public string name { get; protected set; }
    public string pass { get; protected set; }

    public User(int userID)
    {
        id = userID;
    }
    public User(string Username, string Password)
    {
        name = Username;
        pass = Password;
    }

    // Other functions go here...

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing) 
        {
            // free managed resources
        }
        // free native resources if there are any.
    }
}