Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Since a linked list isn't a dictionary, I would <em>expect</em> it to use <code>EqualityComparer&lt;T&gt;.Default.Equals(x,y)</code>.</p> <p>This supports (in order):</p> <ul> <li><code>IEquatable&lt;T&gt;</code> (for the <code>T</code> in question)</li> <li><code>object.Equals</code> (which uses reference equality by default, or supports an overridden <code>Equals(object)</code>)</li> </ul> <p>Note that <code>EqualityComparer&lt;T&gt;</code> also handles the usual <code>null</code> rules etc for both classes and <code>Nullable&lt;T&gt;</code>.</p> <p>(update: checked, and correct; from <code>Find(T)</code>)</p> <pre><code>... EqualityComparer&lt;T&gt; comparer = EqualityComparer&lt;T&gt;.Default; ... if (comparer.Equals(head.item, value)) {...} ... </code></pre> <hr> <p>Update to prove this working when <code>ISomeInterface: IEquatable&lt;ISomeInterface&gt;</code>, as per comments:</p> <pre><code>using System; using System.Collections.Generic; class Program { static void Main() { LinkedList&lt;IFoo&gt; foos = new LinkedList&lt;IFoo&gt;(); 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&lt;IFoo&gt; { 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 + " &gt; override Equals"); return base.Equals(other); } bool IEquatable&lt;IFoo&gt;.Equals(IFoo other) { Console.WriteLine(Value + " &gt; 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 + " &gt; override Equals"); return base.Equals(other); } public bool Equals(IFoo other) { Console.WriteLine(Value + " &gt; implicit Equals"); return base.Equals(other); } public void Bar() { } public override int GetHashCode() { return base.GetHashCode(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload