I'm having a problem in using the Dictionary ContainsKey method. although I'm overriding the Equals
and GetHashCode
methods, I'm always getting the value true.
What am I missing?
The FreeTime objects contains 2 variables of the type DateTime (start and end).
Here is the full class
class FreeTime : IEquatable<FreeTime>
{
#region Constants
private const bool FirstDayOfWeekIsMonday = true;
#endregion
#region Private Variables
private DateTime start;
private DateTime end;
private static bool TodayIsSunday = (int)DateTime.Today.DayOfWeek == 0;
#endregion
#region Public Fields
public DateTime Start { get { return start; } }
public DateTime End { get { return end; } }
public void setStart(DateTime startValue) { start = startValue; }
public void setEnd(DateTime EndValue) { end = EndValue; }
#endregion
#region Constructor
public FreeTime()
{
start = DateTime.Today;
end = DateTime.Today;
}
public FreeTime(DateTime s,DateTime e)
{
start = s;
end = e;
}
#endregion
public enum PeriodType
{
SingleMonth,
AllMonths,
InterveningMonths
}
public bool Equals(FreeTime other)
{
////Check whether the compared object is null.
//if (Object.ReferenceEquals(other, null)) return false;
//Check wether the products' properties are equal.
return start.Equals(other.start) && end.Equals(other.end);
}
public override int GetHashCode()
{
//Get hash code for the start field if it is not null.
int hashStart = start == null ? 0 : start.GetHashCode();
//Get hash code for the end field.
int hashEnd = end.GetHashCode();
//Calculate the hash code .
return hashStart ^ hashEnd;
}
'
this is how I am using the containKey method
Dictionary<FreeTime, string> FreeBusy = new Dictionary<FreeTime, string>();
if (FreeBusy.ContainsKey(intersection))
but I am always geeting the value True