List.Contains 未按预期工作

发布于 2024-09-04 03:19:53 字数 410 浏览 6 评论 0原文

如果我有一个 MyBull 类型的对象和一个 List; orig

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

orig.add(x);

// Again same? data object
MyBull y = getMeTheObjectWithIdFromDB(9);

那为什么这是假的呢?

// This is false, even though all the properties
// of x and y are the same.
orig.Contains<MyBull>(y); 

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

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

orig.add(x);

// Again same? data object
MyBull y = getMeTheObjectWithIdFromDB(9);

Why is this false then?

// This is false, even though all the properties
// of x and y are the same.
orig.Contains<MyBull>(y); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

万水千山粽是情ミ 2024-09-11 03:19:53

默认情况下,对象将公开基于引用的相等性。如果您需要自定义规则,例如基于 id 字段的相等性,则需要重写 EqualsGetHashCode 方法。

By default objects will expose reference based equality. If you want custom rules, such as equality based on id fields, you need to override the Equals and GetHashCode methods.

那片花海 2024-09-11 03:19:53

如果您可以使用 LINQ 那么您可以

class Vessel
{
    public int id { get; set; }
    public string name { get; set; }
}

...

var vessels = new List<Vessel>() { new Vessel() { id = 4711, name = "Millennium Falcon" } };

var ship = new Vessel { id = 4711, name = "Millencolin" };

if (vessels.Any(vessel => vessel.id == ship.id))
    Console.Write("There can be only one!");

If you can use LINQ then you can

class Vessel
{
    public int id { get; set; }
    public string name { get; set; }
}

...

var vessels = new List<Vessel>() { new Vessel() { id = 4711, name = "Millennium Falcon" } };

var ship = new Vessel { id = 4711, name = "Millencolin" };

if (vessels.Any(vessel => vessel.id == ship.id))
    Console.Write("There can be only one!");
撩发小公举 2024-09-11 03:19:53

这是因为 MyBull 实例是通过引用进行比较的。从 .NET 的角度来看,x 和 y 都是不同的实例,因此不等于。

为了解决这个问题,您必须 覆盖 Equals和 GetHashCode 方法(这意味着您应该实现 IEquatable< ;MyBull> 并覆盖 == 和 != 运算符)。

This is because the MyBull instances are being compared by reference. From the point of view from .NET, x and y are both different instances and therefore not Equal.

In order to get around this you will have to override the Equals and GetHashCode methods (which means you should probably implement IEquatable<MyBull> and override the == and != operators too).

随波逐流 2024-09-11 03:19:53

您的 MyBull 对象是否实现了 IEquatable.Equals? 两个对象的相等性

此方法将确定OP 请求的

。您的 MyBull 类将实现 IEquatable

public class MyBull : IEquatable<MyBull>

,然后您需要重写 Equals 方法,

public bool Equals(MyBull theOtherMyBull)

正如 David Neale 在下面提到的,这是当您比较相同类型的对象时(您本身就是这样),最好使用该方法。重写 Object.Equals 和 Object.GetHashCode 也可以。

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

requested by OP

Your MyBull class would implement IEquatable

public class MyBull : IEquatable<MyBull>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文