LinkedList.Contains method. (.NET 2)
How the objects are compared inside? (Equals? CompareTo?)
MSDN tells nothing about.
the situation:
interface IClass
{
string GetName();
}
class Class1 : IClass, IEquatable<Class1>
{
public string FirstName;
public string LastName;
string IClass.GetName() { return FirstName; }
bool IEquatable<Class1>.Equals(Class1 other)
{
return FirstName.Equals(other.FirstName);
}
}
class Class2 : IClass, IEquatable<Class2>
{
public string FirstName;
public string LastName;
string IClass.GetName() { return LastName; }
bool IEquatable<Class2>.Equals(Class2 other)
{
return LastName.Equals(other.LastName);
}
}
public void TestMethod()
{
Class1 c1 = new Class1();
c1.FirstName = "fn";
c1.FirstName = "ln";
Class2 c2 = new Class2();
c2.FirstName = "fn";
c2.FirstName = "ln";
Class1 c3 = new Class1();
c3.FirstName = "fn";
c3.FirstName = "ln";
LinkedList<IClass> myList = new LinkedList<IClass>();
myList.AddFirst(c1);
myList.AddFirst(c2);
// false here
MessageBox.Show("myList<IClass> contains c3? - " + (myList.Contains(c3)));
LinkedList<Class1> myList1 = new LinkedList<Class1>();
myList1.AddFirst(c1);
myList1.AddFirst(c1);
// true here
MessageBox.Show("myList1<Class1> contains c3? - " + (myList1.Contains(c3)));
}