LinkedList.Contains方法。 (.NET 2)LinkedList.Contains。什麼是用來比較對象的方法?
如何在內部比較對象? (Equals?CompareTo?)
MSDN沒有提供任何信息。
的情況:
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)));
}
(答道完整的例子評論) – 2009-11-19 16:01:06
[反射鏡](http://reflector.red-gate.com) – 2009-11-18 11:25:44