我在C#中有一个Address类,如下所示:
public class Address
{
public string StreetAddress { get; set; }
public string RuralRoute { get; set; }
public string City { get; set; }
public string Province { get; set; }
public string Country { get; set; }
public string PostalCode { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我正在实现相等性,所以我需要覆盖哈希码.起初我打算使用EJ的哈希码公式,但后来我想:这些都是字符串字段,我不能只使用StringBuilder连接它们并从该字符串返回哈希码?
那是:
var str = new StringBuilder();
str.Append(StreetAddress)
.Append(RuralRoute)
...
return str.ToString().GetHashCode();
Run Code Online (Sandbox Code Playgroud)
这有什么优点/缺点?我为什么不这样做?