EqualityComparer missing?

enter image description here

I saw an example:

int[] a1 = { 1, 2, 3 }; int[] b1 = { 1, 2, 3 }; a1.Equals(b1) //false a1.Equals(b1,EqualityComparer<int>.Default)); //true 

However, I cannot get the overloaded method, as you see ...

What am I missing?

+4
source share
2 answers

There is no such method on System.Object (or any other type that would allow such use in an ints array). I think you are looking for the Enumerable.SequenceEqual method, an extension method from LINQ to Objects:

 a1.SequenceEqual(b1, EqualityComparer<int>.Default) 

although you can also do this:

 a1.SequenceEqual(b1) 

EDIT: if you want to use the Equals method from IStructuralEquatable , you will have to drop it into the interface, since arrays implement this interface explicitly :

 ((IStructuralEquatable)a1).Equals(b1, EqualityComparer<int>.Default) 
+4
source

The overloaded method you are referring to is an extension method. Intellisense will only show this when declaring the namespace in which the method is declared in your usings block.

However, the method is called SequenceEqual , not just Equals. The method you want is declared in System.Linq .

 using System.Linq; ... a1.SequenceEqual (b1, EqualityComparer<int>.Default); 
+4
source

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


All Articles