C # Tuple Equality vs Lists

I recently came across some amazing Tuple behavior on the list. The list box seems to make a different comparison than what I expected. Can someone explain the difference?

Tuple<int,int> A = new Tuple<int, int>(5,5);
Tuple<int, int> B = new Tuple<int, int>(5, 5);
List<Tuple<int,int>> AList = new List<Tuple<int, int>>() {A};

bool C = AList.Contains(B); // returns true
bool D = A == B; // returns false

Edit 1: To specify a duplicate flag. I knew that == and .Equals were different functions, the amazing thing here is the concrete implementation in the List.Contains function.

+4
source share
4 answers

A == B compares the links, and since they point to two different objects, the result is false. I suppose that was already familiar to you.

bool C = AList.Contains (B); will call .Equal, which checks for equality by returning true.

The difference is the same as A == B vs. A.Equals (B)

+1

, .

, == , , .

Equals true, .

+2

Contains Equals, object

== (.. ).

, Tuple Equals true, , == , .

+1

, .

bool D = A.Equals(B);

It returns truewhat made me see C # the difference between == and Equals ()

Hope this helps

+1
source

Source: https://habr.com/ru/post/1656682/


All Articles