C# i have List<object> with object values internally changed and now i want to remove
C# I have List with object values internally changed and now i want to remove that object what i mean is
class Stre开发者_StackOverflowngth {
public int energy;
public Strength(int energy) {
this.energy = energy;
}
public void increment(){
this.energy++;
}
}
List<Strength> strengthList = new List<Strength>();
Strength strength = new Strength(10);
strengthList.Add(strength);
strength.increment();
strengthList.Remove(strength);
Will it remove the object strength from the strengthList?
Thanks for all the help in advance
What you want to know is how C# handles equality of objects.
In your case, it will remove this object, because they have same reference. You might also override this behaviour in your class, so it is able to compare values of this class.
To add : List.Remove uses EqualityComparer.Default. This will use either IEquatable if object implements it, or will fall back to standard Object.Equals and Object.GetHashCode.
精彩评论