这是预期的 C# 4.0 元组相等行为吗? [英] Is this expected C# 4.0 Tuple equality behavior?

查看:16
本文介绍了这是预期的 C# 4.0 元组相等行为吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到在 .NET 4.0 的两个新 Tuple<> 实例之间使用 .Equals 和 == 之间的行为不同.如果我在 Tuple<> 中的对象上覆盖了 Equals 并在 Tuple 上调用 .Equals ,则将调用 Equals 的覆盖.如果我在元组上使用 ==,则不会调用 Equals 的覆盖.这是设计使然吗?是否有意义?

I'm seeing different behavior between using .Equals and == between two of .NET 4.0's new Tuple<> instances. If I have overridden Equals on the object in the Tuple<> and call .Equals on the Tuples the override of Equals will be called. If I use == on the Tuples the override of Equals is not called. Is that by design and does it make sense?

从答案和评论中我可以看出我并不清楚.我知道 Tuple<> 是一个引用类型,对于引用类型 == 将检查身份(ReferenceEquals).但是,是否应该 Tuple<> 覆盖 == 来检查它包含的对象的相等性?为了一致性,可能不会.

From answers and comments I can tell I'm not being clear. I know Tuple<> is a reference type and that for reference types == will check identity (ReferenceEquals). But, should Tuple<> override == to check equality of the objects it contains? For consistency, probably not.

例如,如果我有一个简单的对象

For example if I have a simple object

public class NameAndNumber
{
    public int Number { get; set; }
    public string Name { get; set; }

    public override bool Equals(object obj)
    {
        if (obj is NameAndNumber)
        {
            NameAndNumber other = (NameAndNumber)obj;
            return Number == other.Number && Name == other.Name;
        }

        return false;
    }
}

然后我做这样的事情:

Tuple<NameAndNumber, NameAndNumber> left = new Tuple<NameAndNumber, NameAndNumber>(
      new NameAndNumber { Name = "one", Number = 1 }, 
      new NameAndNumber { Name = "two", Number = 2 });
Tuple<NameAndNumber, NameAndNumber> right = new Tuple<NameAndNumber, NameAndNumber>(
      new NameAndNumber { Name = "one", Number = 1 }, 
      new NameAndNumber { Name = "two", Number = 2 });
bool operatorResult = left == right;
bool equalsResult = left.Equals(right);
Console.Out.WriteLine("operatorResult = {0}  equalsResult = {1}", 
        operatorResult, equalsResult);

我得到 operatorResult = false equalsResult = true

I get operatorResult = false equalsResult = true

我应该期待吗?

我知道 NameAndNumber 上的 Equals 实现并不正确",它只是简化的示例代码.

I know the implementation of Equals on NameAndNumber isn't "right" it's just simplified sample code.

我也尝试过实现 IEquatable、==、!= 和 GetHashCode.结果相同.

I have also tried implementing IEquatable, ==, !=, and GetHashCode. Same results.

推荐答案

你看到的结果来自一个 设计妥协,元组现在在 F# 和 C# 之间共享.重点是所有元组确实都是作为引用类型实现的,这不是很明显.

The results you see come from a design compromise, Tuples are now shared between F# and C#. The main point is that all Tuples are indeed implemented as reference types, that was not so obvious.

Tuples 是否应该进行深层或浅层相等检查的决定已移至两个接口:IStructuralComparableIStructuralEquatable.请注意,这 2 个现在也由 Array 类实现.

The decision whether Tuples should do deep or shallow equality checks was moved to two interfaces: IStructuralComparable, IStructuralEquatable. Note that those 2 are now also implemented by the Array class.

这篇关于这是预期的 C# 4.0 元组相等行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆