user677607
user677607

Reputation:

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 Strength {
  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

Upvotes: 1

Views: 586

Answers (1)

Euphoric
Euphoric

Reputation: 12849

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.

Upvotes: 1

Related Questions