[ACCEPTED]-Equals method of System.Collections.Generic.List<T>...?-equals
Accepted answer
You can use the Linq method SequenceEqual
on the list 5 since your list implements IEnumerable
. This will verify 4 all the elements are the same and in the 3 same order. If the order may be different, you 2 could sort the lists first.
Here's a minimal 1 example:
using System;
using System.Collections.Generic;
using System.Linq; // for .SequenceEqual
class Program {
static void Main(string[] args) {
var l1 = new List<int>{1, 2, 3};
var l2 = new List<int>{1, 2, 3};
var l3 = new List<int>{1, 2, 4};
Console.WriteLine("l1 == l2? " + l1.SequenceEqual(l2));
Console.WriteLine("l1 == l3? " + l1.SequenceEqual(l3));
}
}
public class MyList<T> : List<T>
{
public override bool Equals(object obj)
{
if (obj == null)
return false;
MyList<T> list = obj as MyList<T>;
if (list == null)
return false;
if (list.Count != this.Count)
return false;
bool same = true;
this.ForEach(thisItem =>
{
if (same)
{
same = (null != list.FirstOrDefault(item => item.Equals(thisItem)));
}
});
return same;
}
}
0
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.