Collection.Contains()用于检查现有对象是什么?(What does Collection.Contains() use to check for existing objects?

编程入门 行业动态 更新时间:2024-10-27 03:42:49
Collection.Contains()用于检查现有对象是什么?(What does Collection.Contains() use to check for existing objects?)

我有一个强类型的自定义对象列表MyObject,它具有属性Id以及其他一些属性。 假设一个MyObject的Id将其定义为唯一的,并且我想检查我的集合是否已经有一个ID为1的MyObject对象,然后再将我的新MyObject添加到集合中。 我想要使​​用if(!List.Contains(myObj)),但我该如何强制实现只有一个或两个MyObject属性将其定义为唯一的事实? 我可以使用IComparable? 或者我只需要重写一个Equals方法,但是我需要先继承一些东西吧?

谢谢

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 Id of a MyObject defines it as unique and I want to check if my collection doesn't already have a MyObject object that has an Id of 1 before I add my new MyObject to the collection. I want to use if(!List.Contains(myObj)) but how do I enforce the fact that only one or two properties of MyObject define it as unique? I can use IComparable? Or do I only have to override an Equals method but I'd need to inherit something first is that right?

Thanks

最满意答案

List<T>.Contains使用EqualityComparer<T>.Default , IEquatable<T>继而使用IEquatable<T>如果类型实现它),否则使用IEquatable<T> 。

你可以实现IEquatable<T>但如果你这样做,重写IEquatable<T>是一个好主意,如果你这样做,重写GetHashCode()是一个很好的主意:

public class SomeIDdClass : IEquatable<SomeIDdClass> { private readonly int _id; public SomeIDdClass(int id) { _id = id; } public int Id { get { return _id; } } public bool Equals(SomeIDdClass other) { return null != other && _id == other._id; } public override bool Equals(object obj) { return Equals(obj as SomeIDdClass); } public override int GetHashCode() { return _id; } }

请注意,哈希码与平等标准相关。 这是至关重要的。

这也使其适用于任何其他情况,其中由具有相同ID定义的相等性是有用的。 如果你有一个检查列表是否有这样一个对象的要求,那么我可能会建议你做:

return someList.Any(item => item.Id == cmpItem.Id);

List<T>.Contains uses EqualityComparer<T>.Default, which in turn uses IEquatable<T> if the type implements it, or object.Equals otherwise.

You could just implement IEquatable<T> but it's a good idea to override object.Equals if you do so, and a very good idea to override GetHashCode() if you do that:

public class SomeIDdClass : IEquatable<SomeIDdClass> { private readonly int _id; public SomeIDdClass(int id) { _id = id; } public int Id { get { return _id; } } public bool Equals(SomeIDdClass other) { return null != other && _id == other._id; } public override bool Equals(object obj) { return Equals(obj as SomeIDdClass); } public override int GetHashCode() { return _id; } }

Note that the hash code relates to the criteria for equality. This is vital.

This also makes it applicable for any other case where equality, as defined by having the same ID, is useful. If you have a one-of requirement to check if a list has such an object, then I'd probably suggest just doing:

return someList.Any(item => item.Id == cmpItem.Id);

更多推荐

本文发布于:2023-07-24 20:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1251001.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   Collection   check   objects   existing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!