I am trying to compare two .NET arrays. Here is an obvious implementation for comparing arrays of bytes:
bool AreEqual(byte[] a, byte[] b){
if(a.Length !
You could use SequenceEqual:
string[] a = { "1", "2", "3" };
string[] b = { "1", "2", "3" };
bool areEqual = a.SequenceEqual(b); // true
string[] c = { "1", "2", "5" };
areEqual = a.SequenceEqual(c); // false
Maybe something like this?
static bool AreEqual<T>(T[] a, T[] b)
{
bool areEqual = false ;
T[] result = a.Intersect(b.AsEnumerable()).ToArray();
areEqual = (result.Length == a.Length) && (result.Length == b.Length);
return areEqual;
}
I am not sure of the performance hit on this one though.
EDIT
revised version taking into account Jon suggestions:
static bool AreEqual<T>(T[] a, T[] b)
{
return a.SequenceEqual(b);
}
With the advent of .NET 4 you can use the method Equals() being provided by .NET arrays' explicitly implemented interface IStructuralEquatable. Then the code might look like this (I rewrote CMS' example):
string[] a = { "1", "2", "3" };
string[] b = { "1", "2", "3" };
bool result = ((IStructuralEquatable)a).Equals(b, StructuralComparisons.StructuralEqualityComparer);
// result evaluates to true.
(IStructuralEquatable is also implemented in Tuples (also new in .NET 4).)
Kathy's approach seems a good one to me. I'd personally allow the comparer to be specified explicitly:
bool AreEqual<T>(T[] a, T[] b)
{
return AreEqual(a, b, EqualityComparer<T>.Default);
}
bool AreEqual<T>(T[] a, T[] b, IEqualityComparer<T> comparer)
{
// Handle identity comparison, including comparing nulls
if (a == b)
{
return true;
}
if (a == null || b == null)
{
return false;
}
if(a.Length != b.Length)
{
return false;
}
for(int i = 0; i < a.Length; i++)
{
if(!comparer.Equals(a[i], b[i]))
{
return false;
}
}
return true;
}
SequenceEqual as mentioned by CMS is good, but due to its generality over IEnumerable<T>
I don't think it can do the "early out" if the length aren't equal. (It's possible that it checks for both sequences implementing IList though, to check Count directly.) You could generalise a little more, to use IList<T>
bool AreEqual<T>(IList<T> a, IList<T> b, IEqualityComparer<T> comparer)
{
if(a.Count != b.Count)
{
return false;
}
for(int i = 0; i < a.Count; i++)
{
if(!comparer.Equals(a[i], b[i]))
{
return false;
}
}
return true;
}
The straight array version will probably be the most efficient - adding generality and abstraction usually hits performance, although whether it's significant will depend on your app.