c# List.Contains() Method Returns False

后端 未结 9 1867
梦毁少年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:49

    You need to implement Equals. Most probably as:

    public override bool Equals(object obj)
    {
         Car car = obj as Car;
         if(car == null) return false;
         return car.CarID == this.CarID && car.CarName == this.CarName;
    }
    

提交回复
热议问题