List.Contains and T[].Contains behaving differently

前端 未结 3 1007
情歌与酒
情歌与酒 2021-02-12 14:25

Say I have this class:

public class Animal : IEquatable
{
    public string Name { get; set; }

    public bool Equals(Animal other)
    {
                 


        
3条回答
  •  后悔当初
    2021-02-12 14:59

    Array has no method with name of contains, this is an extension method from Enumerable class.

    Enumerable.Contains method, which you are using that in your array,

    is using default equality comparer.

    The default equality comparer, needs override of Object.Equality method.

    This is because of backward compatibility.

    Lists have their own specific implementations, but Enumerable should be compatible with any Enumerable, from .NET 1 to .NET 4.5

    Good luck

提交回复
热议问题