Posts

Showing posts from April, 2010

Issue with List.Contains()

Methods such as Contains, IndexOf, LastIndexOf, and Remove use an equality comparer for the list elements. The default equality comparer for type T is determined as follows. If type T implements the IEquatable generic interface, then the equality comparer is the Equals method of that interface; otherwise, the default equality comparer is Object.Equals." So, you should implement the IEquatable interface on your Car object, and tell it to compare the _name elements for the equality test. e.g. (not a complete class !) public class Car : IEquatable { ... protected string _name; public string Name { get { return _name; } ... } #region IEquatable Members public bool Equals(Car other) { bool result = false; if( this.Name == other.Name ) { result = true; } return result; } #endregion }