Since R# 2017.1.1 I get warnings in the automatically generated GetHashCode()
function. Let me explain how this function is created:
If you override the Equals
function of a class, R# suggests you to create equality members. If you let R# generate those equality members, it overrides the GetHashCode()
function as well. In there however it uses all the properties of my class. As those properties aren't all read-only R# tells me that I should make them readonly and displays a warning.
So my question is wether I should just leave the GetHashCode()
function empty (or delete those parts with warnings) or wether I should try to make the properties readonly so that R# isn't warning me anymore.
Here's my code:
public override bool Equals(object obj) => obj is RamEntry && Equals((RamEntry) obj);
public override int GetHashCode()
{
unchecked
{
var hashCode = (Value != null ? Value.GetHashCode() : 0); //In those 5 lines the warnings are being displayed
hashCode = (hashCode * 397) ^ Unimportant.GetHashCode(); //-------------------------------------------------
hashCode = (hashCode * 397) ^ (Comment?.GetHashCode() ?? 0);//-------------------------------------------------
hashCode = (hashCode * 397) ^ (Address?.GetHashCode() ?? 0);//-------------------------------------------------
hashCode = (hashCode * 397) ^ LastUpdated.GetHashCode(); //-------------------------------------------------
return hashCode;
}
}
private bool Equals(RamEntry other)
=> string.Equals(Value, other.Value) && Unimportant == other.Unimportant &&
string.Equals(Comment, other.Comment) && string.Equals(Address, other.Address);
This is the warning R# gives me if I delete the GetHashCode()
function:
And this is the warning it gives me with the GetHashCode()
function: