I have the following code:
List<List<int>> list = new List<List<int>>();
list.Add(new List<int> { 0, 1 });
if (list.Contains(new List<int> { 0, 1 })) // false
...
I’m trying to check whether the list contains {0,1}, but the condition is false (I don’t know why, maybe because the ‘new’ keyword).
If this is not the proper way, I’d like to know how to check that.
Thanks!
List<T>.Containscalls theEquals()method to compare objects.Since the inner
List<T>doesn’t overrideEquals, you get reference equality.You can fix this by creating a custom
IEqualityComparer<List<T>>that compares by value and passing it toContains().You can also just use LINQ: