public interface IEquatable<t>
{
bool Equals(T other);
}
When to use it:Let us say you have a custom Type and you plan to use instances of that type in a collection. If you want to use methods like List<T>.contains or List<T>.IndexOf you will need to way to check if an item in your collection equals the item we are trying to find.To facilitate this your custom Type needs to implement the Equals method of IEquatable<T>.
NOTE: Since we are checking for equality, we need to override the default implementations of Object.Equals and Object.GetHashCode methods so that they are consistent with the IEquatable<T>.Equals method.
public class BasketballStar : IEquatable<BasketballStar>
{
public int Age { get; set; }
public int GamesPlayed { get; set; }
public int PointsScored { get; set; }
//The IEquatable's Equals method with a strongly typed
//input parameter
public bool Equals(BasketballStar input)
{
if(this.GamesPlayed==input.GamesPlayed &&
this.PointsScored==input.PointsScored)
return true;
return false;
}
//overriding the base Equals to keep consistency with the
//Equatable's Equals
public override bool Equals(object input)
{
BasketballStar objStar = input as BasketballStar;
if (objStar == null) return false;
return this.Equals(objStar);
}
/*
override the base GetHashCode to be consistent with the
Equals methods Since we are basing equality based on games
played and point scored, the hash code is also an XOR of
the same properties. So if two objects are equal, the Equals
method must return true and thier GetHashCode values must
be the same
*/
public override int GetHashCode()
{
//the XOR value
return this.GamesPlayed ^ this.PointsScored;
}
}
//calling code
class Program
{
static void Main(string[] args)
{
BasketballStar Jordan = new BasketballStar()
{ Age = 50, GamesPlayed = 900, PointsScored = 55000 };
BasketballStar Kobe = new BasketballStar()
{Age = 35, GamesPlayed = 500, PointsScored = 30000};
BasketballStar Lebron = new BasketballStar()
{ Age = 28, GamesPlayed = 500, PointsScored = 31000 };
BasketballStar Durant = new BasketballStar()
{ Age = 28, GamesPlayed = 400, PointsScored = 20000 };
BasketballStar Jordan2 = new BasketballStar()
{ Age = 50, GamesPlayed = 900, PointsScored = 55000 };
//using a list
List lstStars = new List();
lstStars.Add(Jordan);
lstStars.Add(Kobe);
lstStars.Add(Lebron);
lstStars.Add(Durant);
//contains method internally calls the Equals
//method of IEquatable
Console.WriteLine(lstStars.Contains(Jordan2));
//using a dictionary
Dictionary StarsDictionary = new Dictionary(3);
//Add internally calls GetHashCode and will not let you add two
//keys with the same HashCode
StarsDictionary.Add(Jordan, "Chicago Bulls");
StarsDictionary.Add(Kobe, "Lakers");
StarsDictionary.Add(Lebron, "Miami");
StarsDictionary.Add(Durant, "Oklahoma");
//the contains and containsKey methods both internally call the
//GetHashCode as well as the IEquatable's Equals method
//so both of them need to return consistent values
Console.WriteLine(StarsDictionary.ContainsKey(Jordan2));
Console.WriteLine(StarsDictionary.Contains(new KeyValuePair(Jordan2, "chicago bulls")));
}
}
No comments:
Post a Comment