Basically, I have the following:
class Foo {
public override bool Equals(object obj)
{
Foo d = obj as Foo ;
if (d == null)
return false;
return this.Equals(d);
}
#region IEquatable<Foo> Members
public bool Equals(Foo other)
{
if (this.Guid != String.Empty && this.Guid == other.Guid)
return true;
else if (this.Guid != String.Empty || other.Guid != String.Empty)
return false;
if (this.Title == other.Title &&
this.PublishDate == other.PublishDate &&
this.Description == other.Description)
return true;
return false;
}
}
So, the problem is this: I have an optional field Guidthat is a unique identifier. If this is not specified, I need to try to define equality based on less accurate indicators as an attempt to determine if two objects are equal. This works great, but makes it GetHashCode()dirty ... How do I do this? A naive implementation would be something like this:
public override int GetHashCode() {
if (this.Guid != String.Empty)
return this.Guid.GetHashCode();
int hash = 37;
hash = hash * 23 + this.Title.GetHashCode();
hash = hash * 23 + this.PublishDate.GetHashCode();
hash = hash * 23 + this.Description.GetHashCode();
return hash;
}
But what are the odds of colliding the two types of hashing? Of course, I would not expect it to be 1 in 2 ** 32. This is a bad idea, and if so, how can I do this?