Monday, March 25, 2019

c# - Implementing IDisposable correctly

In my classes I implement IDisposable as follows:



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.
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. A call to Dispose(false) should only clean up native resources. A call to Dispose(true) should clean up both managed and native resources. stman User.cs 10





For reference: CA1063: Implement IDisposable correctly



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 lamens terms what the problem is and/or how IDisposable should be implemented, that will really help!

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...