Other than stepping through the elements one by one, how do I compare two lists of strings for equality (in .NET 3.0):
This fails:
// Expected re
While it does iterate over the collection, this extension method I created does not require the order of the two lists to be the same, and it works with complex types as well, as long as the Equals method is overridden.
The following two lists would return true:
List list1 = new List
{
{ "bob" },
{ "sally" },
{ "john" }
};
List list2 = new List
{
{ "sally" },
{ "john" },
{ "bob" }
};
Method:
public static bool IsEqualTo(this IList list1, IList list2)
{
if (list1.Count != list2.Count)
{
return false;
}
List list3 = new List();
foreach (var item in list2)
{
list3.Add(item);
}
foreach (var item in list1)
{
int index = -1;
for (int x = 0; x < list3.Count; x++)
{
if (list3[x].Equals(item))
{
index = x;
}
}
if (index > -1)
{
list3.RemoveAt(index);
}
else
{
return false;
}
}
return !list3.Any();
}