2009-11-18 55 views
3

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))); 
} 
+0

(答道完整的例子評論) – 2009-11-19 16:01:06

+0

[反射鏡](http://reflector.red-gate.com) – 2009-11-18 11:25:44

回答

1

的方法來實現的是等於。我需要實現基本的Equals方法,而不一定是(?!)通用方法。

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); 
    } 

    public override bool Equals(object obj) 
    { 
     if (obj is Class1) 
     { 
      return this.FirstName.Equals((obj as Class1).FirstName); 
     } 
     else 
     { 
      return base.Equals(obj); 
     } 
    } 
} 
+0

@serhio:一般這樣的東西會被編輯到您的問題中[雖然您可能已經指出了開始] – 2009-11-18 13:21:11

+0

這已經是一個答案我發現,不是問題的一部分。 – serhio 2009-11-19 09:48:11

6

由於鏈表是不是一本字典,我會期望它使用EqualityComparer<T>.Default.Equals(x,y)

此支撐件(按順序):

  • IEquatable<T>(用於所討論的T
  • object.Equals(它默認使用引用相等,或支持一個覆蓋Equals(object)

EqualityComparer<T>也處理通常null規則等這兩個類和Nullable<T>

(更新:檢查和正確的;從Find(T)

... 
EqualityComparer<T> comparer = EqualityComparer<T>.Default; 
... 
if (comparer.Equals(head.item, value)) {...} 
... 

更新,以證明該工作的時候ISomeInterface: IEquatable<ISomeInterface>,按照註釋:

using System; 
using System.Collections.Generic; 


class Program 
{ 
    static void Main() 
    { 
     LinkedList<IFoo> foos = new LinkedList<IFoo>(); 
     foos.AddLast(new Foo1("abc")); 
     foos.AddLast(new Foo2("def")); 
     Console.WriteLine("checking contains..."); 
     bool c = foos.Contains(new Foo1("ghi")); 
     Console.WriteLine("...done"); 
    } 
} 
interface IFoo : IEquatable<IFoo> 
{ 
    void Bar(); 
} 
class Foo1 : IFoo 
{ 
    public string Value { get; set; } 
    public Foo1(string value) { Value = value; } 
    public override bool Equals(object other) 
    { 
     Console.WriteLine(Value + " > override Equals"); 
     return base.Equals(other); 
    } 
    bool IEquatable<IFoo>.Equals(IFoo other) 
    { 
     Console.WriteLine(Value + " > explicit Equals"); 
     return base.Equals(other); 
    } 
    public void Bar() { } 
    public override int GetHashCode() { return base.GetHashCode(); } 
} 
class Foo2 : IFoo 
{ 
    public string Value { get; set; } 
    public Foo2(string value) { Value = value; } 
    public override bool Equals(object other) 
    { 
     Console.WriteLine(Value + " > override Equals"); 
     return base.Equals(other); 
    } 
    public bool Equals(IFoo other) 
    { 
     Console.WriteLine(Value + " > implicit Equals"); 
     return base.Equals(other); 
    } 
    public void Bar() { } 
    public override int GetHashCode() { return base.GetHashCode(); } 
} 
+0

是的,你可以看到我張貼的主題中的代碼? 問題是linkedList包含一個接口。 但是對象也實現了IEquitable接口。 – serhio 2009-11-18 12:02:05

+0

不同'T';他們需要爲'LinkedList '實施'IEquatable '來使用它。 – 2009-11-18 13:17:28

+0

或者更具體地說,您需要'IClass:IEquatable ',因爲它對於所有**'IClass'實現都是正確的。 – 2009-11-18 16:26:09

相關問題