What does Collection.Contains() use to check for existing objects?

前端 未结 6 1174
眼角桃花
眼角桃花 2020-12-03 09:34

I have a strongly typed list of custom objects, MyObject, which has a property Id, along with some other properties.

Let\'s say that the

6条回答
  •  囚心锁ツ
    2020-12-03 10:08

    You can override Equals and GetHashCode, implement an IEqualityComparer and use that in the Contains call, or use an extension method like Any

    if (!myList.Any(obj => obj.Property == obj2.Property && obj.Property2 == obj2.Property2))
       myList.Add(obj2);
    

提交回复
热议问题