Peter Cant
Peter Cant

Reputation: 19

Error comparing arrays in C#: 'System.Array' does not contain a definition for 'sequenceEqual'

I'm reading two Excel worksheets using C# & .NET 4.7.2:

var sheet1 = newWS.UsedRange.Value;
var sheet2 = oldWS.UsedRange.Value;

A watch on the sheet1 (or sheet2) shows an array as expected. But when I try to compare them using:

if (sheet1.SequenceEqual(sheet2))

it fails with 'System.Array' does not contain a definition for 'SequenceEqual'

I have declared:

using System.Linq;

and in fact I have got SequenceEqual to work using a trivial example in the same module:

            string[] array1 = { "dot", "net", "deves" };
            string[] array3 = { "dot", "net", "deves" };
            bool b = array1.SequenceEqual(array3);
            Console.WriteLine(b);

So what am I missing here? Does it not like the arrays being variant? Is System.Array a particular type of array that doesn't have sequenceEqual implemented? It's not implemented in this version of .NET? I have the case wrong??? Or more likely something else entirely?

Any hints appreciated.

(Of course the bigger question is the best way to compare the two arrays, sheet1 & sheet2 -- It looks like SequenceEqual should do what I need, but maybe there's a better way?)

Upvotes: 0

Views: 462

Answers (1)

nvoigt
nvoigt

Reputation: 77304

SequenceEqual is an extension method on IEnumerable<T>. System.Array does not implement the typed IEnumerable<T>, only the untyped IEnumerable. That why your compiler complains.

It's easy to compare two arrays of strings. It is known how to compare two string, the rest of the job can be a nice loop. But how should this method even start to compare two things of untyped type. Two objects of unknown structure? When would two objects even be equal to each other?

If you know what your arrays contain, you can transform it to two typed arrays that contain the values you want to compare and then you can use the method SequenceEqual.

Upvotes: 3

Related Questions