views:

241

answers:

3

In Java, Arrays.equals() allows to easily compare the content of two basic arrays (overloads are available for all the basic types).

Is there such a thing in C# ? Is there any "magic" way of comparing the content of two arrays in C# ?

+20  A: 

You could use SequenceEqual. This works for any IEnumerable<T>, not just arrays.

Quartermeister
Note to OP: SequenceEqual is not part of C#, it's part of .NET.
John Saunders
@John, C# is part of .NET. So by transitivity, `SequenceEqual` is part of C#. ;)
Tim Robinson
and C# is always part of .NET no?
kenny
In what way is C# part of .NET? It's a progrmming language, and .NET is a framework. Is VB.NET part of .NET? F#? IronPython? COBOL.NET?
John Saunders
@Evan: utter nonsense. One is a programming language, the other is a framework. It's the same framework for multiple languages. It's pretty obvious they're not the same thing. Be serious. Is VB.NET the same thing as the .NET Framework? Is F# the same thing as the .NET Framework? How do they differ from C# in that regard?
John Saunders
@Evan: the point is obvious. There is one .NET Framework (per version). There are any number of languages. That simple difference should really be enough to convince you that there's something different between the languages and the Framework. Also, the problem I'm fighting here is the number of people who genuinely don't know that other languages exist that use the .NET Framework. They think of C# like Java - one language to the one runtime, when in fact .NET was designed from the start to support multiple languages.
John Saunders
@Evan: I've got no clue how you can't tell the difference between a programming language and a framework. I think I'll leave it there.
John Saunders
@Evan: yes, it matters. A .NET answer will be of use to coders who write in VB.NET, F#, etc. So if the question isn't specifically about C#, then it should say ".NET" so that developers in other languages will know that it also applies to them.
John Saunders
@John Saunders There... re-tagged .net. That wasn't so hard now was it? :)
Evan Plaice
@Evan: thanks. That feels much better.
John Saunders
+6  A: 

Use SequenceEqual in Linq.

int[] arr1 = new int[] { 1,2,3};
int[] arr2 = new int[] { 3,2,1 };

Console.WriteLine(arr1.SequenceEqual(arr2)); // false
Console.WriteLine(arr1.Reverse().SequenceEqual(arr2)); // true
John Buchanan
+1  A: 

Also for arrays (and tuples) you can use new interfaces from .NET 4.0: IStructuralComparable and IStructuralEquatable. Using them you can not only check equality of arrays but also compare them.

static class StructuralExtensions
{
    public static bool StructuralEquals<T>(this T a, T b)
        where T : IStructuralEquatable
    {
        return a.Equals(b, StructuralComparisons.StructuralEqualityComparer);
    }

    public static int StructuralCompare<T>(this T a, T b)
        where T : IStructuralComparable
    {
        return a.CompareTo(b, StructuralComparisons.StructuralComparer);
    }
}

{
    var a = new[] { 1, 2, 3 };
    var b = new[] { 1, 2, 3 };
    Console.WriteLine(a.Equals(b)); // False
    Console.WriteLine(a.StructuralEquals(b)); // True
}
{
    var a = new[] { 1, 3, 3 };
    var b = new[] { 1, 2, 3 };
    Console.WriteLine(a.StructuralCompare(b)); // 1
}
desco