ID プロパティが null になる可能性があることを考えると、次のうちどれが正しい/より良いものですか。
public override int GetHashCode()
{
if (ID == null) {
return base.GetHashCode();
}
return ID.GetHashCode();
}
また
public override int GetHashCode()
{
if (ID != null) {
return ID.GetHashCode();
}
return 0;
}
更新 1: 2 番目のオプションを更新しました。
更新 2: 以下は Equals の実装です。
public bool Equals(IContract other)
{
if (other == null)
return false;
if (this.ID.Equals(other.ID)) {
return true;
}
return false;
}
public override bool Equals(object obj)
{
if (obj == null)
return base.Equals(obj);
if (!obj is IContract) {
throw new InvalidCastException("The 'obj' argument is not an IContract object.");
} else {
return Equals((IContract)obj);
}
}
そしてIDはstring
型です。