Reputation: 53
I am running into an issue that I cannot seem to wrap my head around. I have two lists of the same type and I need to check if those lists match. I am running into a problem in regards to the commented out for loop and the now in use nest foreach loop. I don't need to use both, one was me trying to figure this out.
I have ran this through the debugger and found both lists hold the same data at, what I am assuming (using VS2010 debugger) is at the same element in the list. However, when it is being looped through, it fails at the first element.
I have researched this from similar questions and I am not very familiar with LINQ expressions (which I am making a note to research that in my spare time).
private bool ListMatch(List<T> list1, List<T> list2)
{
if (list1 == null && list2 == null)
{
return true;
}
if ((list1 == null) || (list2 == null))
{
return false;
}
if (list1.Count != list2.Count)
{
return false;
}
if( list1.Equals( list2 ) )
{
return true;
}
/*for (var idx = 0; idx < list1.Count; idx++)
{
if( list1[ idx ] != list2[ idx ] )
{
return false;
}
}*/
foreach( var x in list1 )
{
bool hasDuplicates = false;
foreach( var y in list2 )
{
if( x == y )
{
hasDuplicates = true;
break;
}
}
if( hasDuplicates )
{
return true;
}
return false;
}
return true;
}
Upvotes: 2
Views: 640
Reputation: 241641
You shouldn't be using ==
as that is reference equality by default and it sounds like your algorithm is failing because you really want value equality; use Object.Equals
and override it. That said, the framework does this for you (assuming that you want the two lists to have the same items in the same order). Replace your loop and everything after with:
return list1.SequenceEqual(list2);
You need to make sure that Object.Equals
is overridden for the type of elements in your list (or they implement IEquatable<T>
) , or you'll run into the same issue as one of the issues in your code.
Alternatively, there is an overload that lets you pass in how to compare for equality I'd you need something special or don't want to to override Object.Equals
.
Upvotes: 0
Reputation: 221
You are comparing the references of the list in those indexes. You could do this like this The Except operator will return a list containing those elements which aren't present in both lists, ie the differences. This is useful should you need to use them for something
List<T> result = list1.Except(list2).ToList();
if (result.count == 0)
//No differences
return true;
else
//Differences
return false;
Upvotes: 0
Reputation: 236218
You can use Enumerable.SequenceEqual after you made manual pre-check of equality:
private bool ListMatch(List<T> list1, List<T> list2)
{
if (list1 == null && list2 == null)
return true;
if ((list1 == null) || (list2 == null))
return false;
if (list1.Count != list2.Count)
return false;
if(list1.Equals(list2))
return true;
return Enumerable.SequenceEqual(list1, list2);
}
Upvotes: 3
Reputation: 8026
The second loop has many bugs, don't use it ^^
You can use list1.SequenceEqual(list2)
for checking list equality (assuming the order is important, which it appears to be).
Upvotes: 1