9
I’m using Resharper to automate the method override Equals
and the operator ==
. One of the methods the tool has overwritten is GetHashCode
:
public override int GetHashCode()
{
unchecked
{
return (Id.GetHashCode() * 397) ^ (Name != null ? Name.GetHashCode() : 0);
}
}
What is the function of this method and what is its role in comparing objects in the .NET Framework? What is the function of the reserved word unchecked
? Why multiply the value of HashCode
of property Id by 397? (The project is configured for the version 4.5.1 of .NET Framework).
Complete code of the class:
public class Class1 : IEquatable<Class1>
{
public Guid Id { get; set; }
public string Name { get; set; }
public bool Equals(Class1 other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Id.Equals(other.Id) && string.Equals(Name, other.Name);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Class1) obj);
}
public override int GetHashCode()
{
unchecked
{
return (Id.GetHashCode() * 397) ^ (Name != null ? Name.GetHashCode() : 0);
}
}
public static bool operator ==(Class1 left, Class1 right)
{
return Equals(left, right);
}
public static bool operator !=(Class1 left, Class1 right)
{
return !Equals(left, right);
}
}
I think at the very least there are some inaccuracies in this, not to mention that it’s wrong.
– Maniero
Could you clarify these inaccuracies?
– Marcell Alves
I am willing to correct any information... I have put the information I believe to be correct in my answer. If you have any information or improvement for future understanding please indicate so that we can contribute together.
– Bruno dos Santos Bermann
@Marcellalves
O método GetHashCode é utilizado para comparação entre valores de objetos
, not for this, in my answer has to what is and details on the link.método Equals herdado e não sobrescrito da classe Object que compara entidades por referência (compara o endereço de memória de dois objetos)
, is also wrong, theEquals()
compare what he wants to buy, may even be the reference, but usually it is not.provavelmente uma sequencia interna do Resharper incrementada para cada novo GetHashCode gerado
has nothing to do with Resharper.– Maniero
o resultado desta multiplicação potencialmente poderia exceder limites de memória
ñ is well this.Todo método Equals que tenha por intenção comparar valores e não referências a locais de memória deve comparar o retorno de GetHashCode para os objetos de entrada
this insists on the error.– Maniero
Thanks for the corrections, @bigown. I changed the acceptance of the answer, but I still think it valid Bruno edit your answer with the corrections you put.
– Marcell Alves
Bigown, the comment on the Equals method of the Object class is incorrect. I am sure that this method if no override is made compares memory addresses, which may or may not be true if there is override. Also according to the same comment his explanation of prime numbers is more explanatory than my suggestion (I put as probably because I am not sure of this part).
– Bruno dos Santos Bermann
About the comment regarding the memory overflow... yes this is the usability of the keyword "unchecked" which by the way your answer ignores this part of the question. I ask you to suggest a better explanation for using this keyword that allows anyone who reads your answer to understand the code instead of simply allowing you to copy it without understanding.
– Bruno dos Santos Bermann
@Brunodossantosbermann is true, I didn’t notice the nay superscript, I’m sorry for that flaw, the rest is really wrong. There is no memory overflow, there may be value arithmetic overflow and the signal bit affected. I put in the answer. Then I can improve something, on link has more information about the calculation in Jon Skeet’s answer.
– Maniero
Anyway thank you, later I will make the adjustments in response as proposed.
– Bruno dos Santos Bermann