LinkedList.Contains. What is the method used to compare objects?

LinkedList.Contains Method. (.NET 2)

How are objects compared internally? (Equals? CompareTo?)

MSDN doesn't say anything.

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)));
}
+3
source share
2 answers

The method of implementation is equal. I need to implement the basic Equals method, and not necessarily (?!) A common one.

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

Since the linked list is not a dictionary, I would expect it to use EqualityComparer<T>.Default.Equals(x,y).

This supports (in order):

  • IEquatable<T>(for T)
  • object.Equals(which uses referential equality by default or supports redefinition 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(); }
}
+6

Source: https://habr.com/ru/post/1723215/


All Articles