If you compare the Id property enough to say that the car is equal to another in order to avoid any kind of loop, you can redefine the list with your class that keeps track of the elements and uses IEqualityComparer for the entire collection, for example:
class CarComparer : IList<Car>, IEquatable<CarComparer> { public bool Equals(CarComparer other) { return object.Equals(GetHashCode(),other.GetHashCode()); } public override int GetHashCode() { return _runningHash; } public void Insert(int index, Car item) {
Then you just need to override Add , Remove , etc. and any other methods that may affect the items in the list. Then you can save a private variable, which is a hash of some identifier of the elements in the list. When overriding Equals methods, you can simply compare this private variable. Not the cleanest approach to date (since you need to keep up with your hash variable), but this will result in you not having to iterate over loops for comparison. If it were me, I would just use Linq, as some mentioned here ...
source share