c# List.Contains() Method Returns False

后端 未结 9 1970
梦毁少年i
梦毁少年i 2021-01-28 06:04

In the code block below I would expect dictCars to contain: { Chevy:Camaro, Dodge:Charger }

But, dictCars comes back empty. Because this line returns false each time it

9条回答
  •  耶瑟儿~
    2021-01-28 06:40

    You can add this code, by implementing IEquatable

    public class Car: IEquatable
    {
    
        ......
    
        public bool Equals( Car other )
        {
            return this.CarID  == other.CarID && this.CarName == other.CarName;
        }
    }
    

    Link : http://msdn.microsoft.com/fr-fr/library/vstudio/ms131187.aspx

提交回复
热议问题