I am trying to calculate a hash code for one of my classes, the problem is: this class has a byte[] which returns a different hash for every object. I have made 2 objects of the class and both are instantiated with a new byte[0].
When overriding the Equals operator I've used Enumberable.SquenceEqual() to make sure the content of both array's are the same. But how would I make sure the arrays return the same hashcode if their contents are the same?
my code:
public override bool Equals(object obj)
{
return Equals(obj as MessageType);
}
{
if (messageType != null)
{
return (this.Identification == messageType.Identification) &&
(this.ActivateWindow == messageType.ActivateWindow) &&
(this.Logging == messageType.Logging) &&
(Enumerable.SequenceEqual(this.Pictogram, messageType.Pictogram) == true) &&
(this.Priority == messageType.Priority) &&
(this.Procedure == messageType.Procedure);
}
return false;
}
public override int GetHashCode()
{
var result = this.Identification != null ? this.Identification.GetHashCode() : 0;
result = (result * 397) ^ this.ActivateWindow.GetHashCode();
result = (result * 397) ^ this.Logging.GetHashCode();
result = (result * 397) ^ ((this.Pictogram != null) ? this.Pictogram.GetHashCode() : 0);
result = (result * 397) ^ this.Priority.GetHashCode();
result = (result * 397) ^ ((this.Procedure != null) ? this.Procedure.GetHashCode() : 0);
return result;
}