OK; that is going to break when nulls are involved, so simply deal with
those separately (see below); most of the code is AreEqual - first we handle
"are they the same instance" (which also handles "are they both null") - if
so, must be the same. Then if *either* is null (by itself) then they can't
be equal. So now we've excluded every case involving null; *now* we can
worry about the field equality.
Note also that I've made Equals() consistent with these rules (reports false
if it is compared to something that isn't a Slot), and I've provided a
meaningful hash implementation (so you can use the object in a
dictionary/hashtable).
Does that work any better? To be honest, I'm not 100% sure how far you got,
so rather than me prattle on, if you have any specific questions, please
ask?
Marc
using System;
class Slot : IEquatable<Slot>
{
private int startTimeId, endTimeId;
public override int GetHashCode()
{
return (17 * startTimeId.GetHashCode()) + endTimeId.GetHashCode();
}
public override bool Equals(object obj)
{
Slot slot = obj as Slot;
return slot == null ? false : AreEqual(this, slot);
}
public bool Equals(Slot slot)
{
return AreEqual(this, slot);
}
private static bool AreEqual(Slot s1, Slot s2)
{
if (ReferenceEquals(s1, s2)) return true;
if (ReferenceEquals(s1, null)
|| ReferenceEquals(s2, null)) return false;
return s1.startTimeId == s2.startTimeId
&& s1.endTimeId == s2.endTimeId;
}
public static bool operator ==(Slot s1, Slot s2)
{
return AreEqual(s1, s2);
}
public static bool operator !=(Slot s1, Slot s2)
{
return !AreEqual(s1, s2);
}
}