在C#中,为什么就等于阵列()方法只比较它们的引用,而不是他们的实际内容 [英] In C#, why Equals() method on arrays only compare their references, not their actual content

查看:168
本文介绍了在C#中,为什么就等于阵列()方法只比较它们的引用,而不是他们的实际内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#,为什么等于()方法总是检查两个阵列之间的平等由而不是由内容比较比较引用?

In C#, why Equals() method always check equality between two arrays by comparing the references the and not by comparing the content ?

因此​​,所有的调用方法等于()在执行(很多)如预期与阵列(它不比较的内容)不工作:

As a consequence, all methods calling Equals() in their implementation (a lot) does not work as expected with arrays (it does not compare the content) :

例如:

int[] array1 = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] array2 = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9};

var u = array1.Equals(array1);                                       //true
var v = array1.Equals(array2);                                       //false
var w = Array.Equals(array1, array2);                                //false
var x = (new List<int[]>(new int[][] { array1 })).Contains(array2);  //false
var y = (new int[][] { array1 }).Any(x => x == array2);              //false
var z = (new int[][] { array1, array2 }).Distinct().Count() == 1;    //false

要处理数组(没有母校的类型)一个可能的通用方法可以是:

A possible generic way to handle arrays (no mater the type) could be :

的Object.Equals():如果这两种类型的比较是:(长度相同)阵列,列举项目(总是可能的),对于每个项目,调用的equals()。如果这些调用之一返回,数组是不同的(符),否则返回

In Object.Equals() : if both types to compare are arrays (of same length), enumerate items (always possible), for each item, call Equals(). If one of these calls return false, array are different (return false) otherwise return true.

注:我知道关于 SequenceEqual() memcmp()等方式来比较两个数组。我的问题不是如何比较数组。我只是想知道为什么ç设计者不选择实施全阵列comparaison的equals()方法。

Note : I know about SequenceEqual(), memcmp() and other ways to compare two arrays. My question is not about how to compare arrays. I just want to know why C# designers dont choose to implement a full array comparaison in Equals() method.

推荐答案

尽管微软的框架类是关于什么的Object.Equals(对象)手段可惜有点不一致,一般 X.Equals(Y)将只有更换任意引用 X 与引用<$是真实的C $ C>是,和/或反之亦然,预期不会改变有关的对象的语义。例如,如果 X 字符串与内容你好,而是一个不同的字符串与相同的内容,与其他替代引用参考文献,将一个字符串一般不会改变他们的行为。虽然code,它使用的ReferenceEquals 来测试两个字符串的引用是否引用相同的字符串可能会注意到开关,正常的字符串code不会。

Although Microsoft's Framework classes are unfortunately a bit inconsistent with regard to what Object.Equals(Object) means, in general X.Equals(Y) will only be true if replacing arbitrary references to X with references to Y, and/or vice versa, would not be expected to alter the semantics of the objects in question. For example, if X is a String with the content "Hello", and Y is a different string with that same content, replacing references to one string with references to the other would not generally alter their behavior. Although code which uses ReferenceEquals to test whether two string references refer to the same string might notice the switch, normal string code would not.

作为一般规则,没有可变对象是等同于任何其它,并且因此除非两个引用参考的相同的目的是可变对象没有提及应相当于另一个。还有就是 INT [] ,两者都持有相同的价值观,对有两个引用同一个实例的两个不同的实例引用有很大的区别。虽然这将是阵列有益有这将测试 ItemsEqual 方法是否阵列的所有物品,或者一定范围项目,匹配,这将有助于有一个 ImmutableArray 类型等于 / 这会认为具有相同内容相等的两个不变的阵列GetHash code 的成员,这是完全正确的,恰当的不同的可变阵列的内容,不管比较不等对方。

As a general rule, no mutable object is equivalent to any other, and thus no reference to a mutable object should be equivalent to another unless both references refer to the same object. There is a big difference between having references to two different instances of int[], both of which hold the same values, versus having two references to the same instance. While it would be helpful for Array to have ItemsEqual methods which would test whether all items of the arrays, or certain ranges of items, matched, and it would be helpful to have an ImmutableArray type with a Equals/GetHashCode members that would regard as equal two immutable arrays with the same content, it is entirely right and proper that distinct mutable arrays compare unequal to each other regardless of content.

这篇关于在C#中,为什么就等于阵列()方法只比较它们的引用,而不是他们的实际内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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