Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List<T> operator == In the C# Language Specification Version 4

Tags:

In the C# Language Specification Version 4, 1.6.7.5 Operators is information about List<T> operators: == and !=. But I can't find such operators defined in List<T>? Am I missing something?

Sample code from 1.6.7.5 Operators:

List<int> a = new List<int>(); a.Add(1); a.Add(2); List<int> b = new List<int>(); b.Add(1); b.Add(2); Console.WriteLine(a == b); // Outputs "True" => here I get False as well b.Add(3); Console.WriteLine(a == b); // Outputs "False" 
like image 308
Daniel Dušek Avatar asked Aug 07 '12 19:08

Daniel Dušek


People also ask

What is List t?

The List<T> is a collection of strongly typed objects that can be accessed by index and having methods for sorting, searching, and modifying list. It is the generic version of the ArrayList that comes under System.

What is this operator in C#?

The “this” keyword in C# is used to refer to the current instance of the class. It is also used to differentiate between the method parameters and class fields if they both have the same name. Another usage of “this” keyword is to call another constructor from a constructor in the same class.


2 Answers

List<T> is a reference type which does not overload operator==. Therefore, it uses the default reference equality semantics. You seem to be under the impression that it overrides operator== to provide value semantics, but it does not. a will equal b when a and b both refer to the same List<T> instance.

EDIT: So I looked at the spec myself. It says:

The List class declares two operators, operator == and operator !=, and thus gives new meaning to expressions that apply those operators to List instances. Specifically, the operators define equality of two List instances as comparing each of the contained objects using their Equals methods. The following example uses the == operator to compare two List instances.

Honestly... I have no clue what they're talking about, but this does not appear to be correct. As far as I can tell after running a few tests the List<T> class uses reference equality. Good question.

EDIT2: Decompiled List<T>, no operator== and/or operator!= overload. The spec appears to be completely incorrect in this case.

like image 56
Ed S. Avatar answered Oct 18 '22 18:10

Ed S.


The spec is indeed correct, although confusing. The spec defines a class called List (poor naming choice).

The following table shows a generic class called List, which implements a growable list of objects. The class contains several examples of the most common kinds of function members.

This class can be seen in the spec at section 1.6.7. The Equals operator is overloaded and matches the output explained above. Perhaps a better name should have been chosen for that class.

static bool Equals(List<T> a, List<T> b) {     if (a == null) return b == null;     if (b == null || a.count != b.count) return false;     for (int i = 0; i < a.count; i++) {         if (!object.Equals(a.items[i], b.items[i])) {             return false;         }     }   return true; } 
like image 43
Dylan Meador Avatar answered Oct 18 '22 19:10

Dylan Meador