List.Contains is not working as hoped

前端 未结 4 1135
[愿得一人]
[愿得一人] 2020-12-06 08:28

If I have an object of type MyBull and a List orig:

// Just an example
MyBull x = getMeTheObjectWithIdFromDB(9);

ori         


        
4条回答
  •  隐瞒了意图╮
    2020-12-06 08:45

    Does your MyBull object implement IEquatable.Equals? This method will determine the equality of two objects

    requested by OP

    Your MyBull class would implement IEquatable

    public class MyBull : IEquatable
    

    and then you would need to override the Equals method

    public bool Equals(MyBull theOtherMyBull)
    

    As David Neale mentions below, this is best used when you're comparing objects of the same type--which you are. Overriding Object.Equals and Object.GetHashCode will work too.

提交回复
热议问题