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
}

Comments

Popular posts from this blog

WCF WSDL location address Issue resolved when hosted over HTTPS with basicHTTPBinding

Yellow Background issue in Word to PDF conversion

Gmail tricks - create unlimited siblings of your Gmail address