正確實現 IDisposable - Implementing IDisposable correctly

問題:

In my classes I implement IDisposable as follows:在我的課程中,我按如下方式實現IDisposable

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()
    {
        // Clear all property values that maybe have been set
        // when the class was instantiated
        id = 0;
        name = String.Empty;
        pass = String.Empty;
    }
}

In VS2012, my Code Analysis says to implement IDisposable correctly, but I'm not sure what I've done wrong here.在 VS2012 中,我的代碼分析說要正確實現 IDisposable,但我不確定我在這裏做錯了什麼。
The exact text is as follows:確切的文字如下:

CA1063 Implement IDisposable correctly Provide an overridable implementation of Dispose(bool) on 'User' or mark the type as sealed. CA1063 正確實現 IDisposable 在“用戶”上提供可覆蓋的 Dispose(bool) 實現或將類型標記爲密封。 A call to Dispose(false) should only clean up native resources.調用 Dispose(false) 應該只清理本機資源。 A call to Dispose(true) should clean up both managed and native resources.調用 Dispose(true) 應該清理託管資源和本機資源。 stman User.cs 10 stman 用戶.cs 10

For reference: CA1063: Implement IDisposable correctly供參考: CA1063:正確實施 IDisposable

I've read through this page, but I'm afraid I don't really understand what needs to be done here.我已經通讀了這個頁面,但恐怕我真的不明白這裏需要做什麼。

If anyone can explain in more layman's terms what the problem is and/or how IDisposable should be implemented, that will really help!如果有人可以更通俗地解釋問題是什麼和/或應該如何實施IDisposable ,那真的很有幫助!


解決方案:

參考一: https://en.stackoom.com/question/1EwFk
參考二: https://stackoom.com/question/1EwFk
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章