. net – LinkedList. Contains. What is the method used to compare objects?
•
Java
LinkedList. Contains method (.NET 2)
How to compare objects in it? (equal to "CompareTo")
MSDN knows nothing about it
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
Message@R_527_2419@.Show("myList<IClass> contains c3? - " + (myList.Contains(c3)));
LinkedList<Class1> myList1 = new LinkedList<Class1>();
myList1.AddFirst(c1);
myList1.AddFirst(c1);
// true here
Message@R_527_2419@.Show("myList1<Class1> contains c3? - " + (myList1.Contains(c3)));
}
Solution
Since the linked list is not a dictionary, I want it to use equality comparer < T > Default. Equals(x,y).
This supports (in order):
>Iequatable < T > (for problematic T) > object Equals (reference equality is used by default, or overridden equals (object) is supported)
Note that equalitycomparer < T > also handles these two classes and the common null rules of nullable < T >
(update: check, correct; from find (T))
...
EqualityComparer<T> comparer = EqualityComparer<T>.Default;
...
if (comparer.Equals(head.item,value)) {...}
...
Update to prove this in isomeinterface: iequatable < isomeinterface & gt;, According to comments:
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(); }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
