Here's a simple class and implementation of equality comparers.
As you can see, the standard apporach for equals is to make sure they are of the same time first, and then, that the inside matches (in our case, a string and a date).
If you want something else, you can always override it to your heart content, and cast both sides to something you're happy with :)
public struct InputEntry
{
public DateTime Date { get; set; }
public string Entry { get; set; }
public bool Equals(InputEntry other)
{
return Date.Equals(other.Date) && string.Equals(Entry, other.Entry);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is InputEntry && Equals((InputEntry) obj);
}
public override int GetHashCode()
{
unchecked
{
return ( Date.GetHashCode()*397)
^ (Entry != null ? Entry.GetHashCode()
: 0);
}
}
public static bool operator ==(InputEntry left, InputEntry right)
{
return left.Equals(right);
}
public static bool operator !=(InputEntry left, InputEntry right)
{
return !left.Equals(right);
}
private sealed class EntryDateEqualityComparer
: IEqualityComparer<InputEntry>
{
public bool Equals(InputEntry x, InputEntry y)
{
return string.Equals(x.Entry, y.Entry) && x.Date.Equals(y.Date);
}
public int GetHashCode(InputEntry obj)
{
unchecked
{
return ( (obj.Entry != null ? obj.Entry.GetHashCode() : 0)*397)
^ obj.Date.GetHashCode();
}
}
}
private static readonly IEqualityComparer<InputEntry>
EntryDateComparerInstance = new EntryDateEqualityComparer();
public static IEqualityComparer<InputEntry> EntryDateComparer
{
get { return EntryDateComparerInstance; }
}
}
ushort
. This should be the reason – Cortexx.Equals(y)
to be true, butx.GetType() == y.GetType()
to be false. – HeavehoSquare s
with a side of 4 and a 4x4Rectangle r
. Its reasonable (though it would depend on the case) fors.equals(r)
to be true, but fors.GetType() == y.GetType()
to be false. – Ulcerationsquare
class in the first place. But the main reason against doing that is that it's hard to maintain the rules of reflexivity and transitivity when working with multiple classes assuming those classes have the same aspects (width and height in this case). And it is impossible when they have different aspects. – Mouseear