Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, why Equals() method on arrays only compare their references, not their actual content

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) :

Example :

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 :

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.

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.

like image 684
tigrou Avatar asked Feb 19 '13 14:02

tigrou


People also ask

What is '~' in C language?

The tilde (~) is a character in the standard ASCII character set that is provided on a conventional computer keyboard and is used in both writing and computer programming. It corresponds to ASCII code 126. The tilde is also sometimes known as the twiddle.

What does += mean in C?

+= Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. C += A is equivalent to C = C + A. -=

What does the || mean in C?

The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.

What is operators in C?

An operator is a symbol that operates on a value or a variable. For example: + is an operator to perform addition. C has a wide range of operators to perform various operations.


1 Answers

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.

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.

like image 64
supercat Avatar answered Oct 02 '22 14:10

supercat