Consider this code:
public static void Main()
{
var item = new Item { Id = 1 };
IList list = new List<Item> { item };
IList array = new[] { item };
var newItem = new Item { Id = 1 };
var lIndex = list.IndexOf(newItem);
var aIndex = array.IndexOf(newItem);
Console.WriteLine(lIndex);
Console.WriteLine(aIndex);
}
public class Item : IEquatable<Item>
{
public int Id { get; set; }
public bool Equals(Item other) => other != null && other.Id == Id;
}
Results:
0
-1
Why are the results different between List<T>
and Array
? I guess this is by design, but why?
Looking at the code of List<T>.IndexOf
makes me wonder even more, since it's porting to Array.IndexOf
.