问题:
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