2013年8月28日星期三

Why should also override Equals override GetHashCode

 

. NET programmer knows that if we override the Equals method of a class which does not override GetHashCode, then VS will prompt a warning: "***" rewrite Object.Equals (object o) but does not override the Object . GetHashCode ().

 

But why override Equals must also override GetHashCode it?

 

Microsoft's explanation is:

 

GetHashCode ; returns a value, based on the current instance, that is suited for hashing algorithms and data structures such as a hash table. "> GetHashCode based on suitable hashing algorithms and data structures such as a hash table of the current instance returns a value. two equal objects of the same type must return the same hash code, in order to ensure correct operation of the following types of examples:

   

link: http://msdn.microsoft.com/zh-cn/library/vstudio / ms182358.aspx

 

For example:

 

rewrite a Person class

 
  
 public class Person 
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }

/// <summary>
/// 重写Equals,如果Name与Age相等,就认为类相等
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (obj == null)
return false;
if (obj is Person)
{
var person = obj as Person;
return person.Age == this.Age && person.Name == this.Name;
}
else
return false;
}
}
 
 

write a test method:

 
  
  public  static void Main(string[] args) 
{
Person person1
= new Person() { Id = 1, Name = "AA", Age = 21 };
Person person2
= new Person() { Id = 2, Name = "AA", Age = 21 };

Console.WriteLine(
"person1与person2是否相等:" + person1.Equals(person2));
Console.Read();
}
 
 

Results:

 

 

result returned is true, it seems to be our overriding methods succeeded. Then we continue to write a test method it.

 
  
            ICollection<Person> list = new HashSet<Person>(); 
list.Add(person1);
Console.WriteLine(
"List是否包含person1:"+list.Contains(person1));
Console.WriteLine(
"List是否包含person2:" + list.Contains(person2));
 
 

Results:

 

 

At this time, on the problem. Since person1 and person2 equal, list it contains person1, it should also include person2 it. The reason is that we do not override GetHashCode method returns the same object is not equal HashCode.

 

we re a change of Person, let it override the GetHashCode method.

 
  
 public class Person 
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }

/// <summary>
/// 重写Equals,如果Name与Age相等,就认为类相等
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (obj == null)
return false;
if (obj is Person)
{
var person = obj as Person;
return person.Age == this.Age && person.Name == this.Name;
}
else
return false;
}

public override int GetHashCode()
{
return this.Name.GetHashCode()^this.Age.GetHashCode();
}
}
 
 

then run the two test methods:

 

 

Now, the method returns true.

 

another example, if the Person class as a key in the dictionary will have a problem, you can see: http://book.51cto.com/art/201109/292340.htm .

 

override GetHashCode principle is very simple, as long as the guarantee of the same type two equal objects return the same hash code on OK.

 

also find a place to put it better, which second comment is very exciting. http://blog.csdn.net/chenyuxu0/article/details/5886771

没有评论:

发表评论