Bug 5245 - IEquatable<T> , IEqualityComparer<T> are ignored by LinkedList<T>.Find
Summary: IEquatable<T> , IEqualityComparer<T> are ignored by LinkedList<T>.Find
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: master
Hardware: PC Windows
: High normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2012-05-22 14:25 UTC by Ruofeng Lin
Modified: 2014-01-14 08:26 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Ruofeng Lin 2012-05-22 14:25:07 UTC
I recently ported some code from Standard desktop .Net Framework to Mono, and I found some strange behaviour of LinkedList<T>.
It just ignored my custom Equals function, which is the implementation of IEquatable<T> , IEqualityComparer<T> interface.
So I can't find any node by using LinkedList<T>.Find() method.

This worked in standard .net framework.

Then I noticed if I override the non generic Object.Equals(object) function it'll then work.
But I think it is also very important to support IEquatable<T> and IEqualityComparer<T> interface
Comment 1 Jonathan Pryor 2014-01-13 15:10:53 UTC
LinkedList<T>.Find() (and maybe others?) should use EqualityComparer<T>.Default.Equals(T, T), not object.Equals(object):

https://github.com/mono/mono/blob/dab4dc95/mcs/class/System/System.Collections.Generic/LinkedList.cs#L236

Sample program:

  using System;
  using System.Collections.Generic;

  class MyValue : IEquatable<MyValue> {
    public readonly string Value;
  
    public MyValue (string value)
    {
      this.Value = value;
    }
  
    public bool Equals (MyValue other)
    {
      if (other == null)
        return false;
      if (object.ReferenceEquals (this, other))
        return true;
      return string.Equals (Value, other.Value,
          StringComparison.OrdinalIgnoreCase);
    }
  }

  class TEst {
    public static void Main ()
    {
      var list = new LinkedList<MyValue> ();
      list.AddFirst (new MyValue ("first"));
      var expected = new MyValue ("FIRST");
      var n = list.Find (expected);
      if (n == null)
        throw new InvalidOperationException ("Expected MyValue!");
      Console.WriteLine (n);
    }
  }

Expected behavior: Same as .NET:

  System.Collections.Generic.LinkedListNode`1[MyValue]

Actual behavior:

> Unhandled Exception:
> System.InvalidOperationException: Expected MyValue!
>   at TEst.Main () [0x00000] in <filename unknown>:0
Comment 2 Marek Safar 2014-01-14 08:26:05 UTC
Fixed in master