I want to use a date range (from one date to another date) as a key for a dictionary, so I wrote my own struct:
struct DateRange
{
public DateTime St
Since DateTime.GetHashCode is internally based on Ticks, what about this:
public override int GetHashCode()
{
return unchecked((int)(Start.Ticks ^ End.Ticks));
}
Or, since you seem to be interested by the date parts (year, month, day), not the whole thing, this implementation uses the number of days between the two dates and should give almost no collision:
public override int GetHashCode()
{
return unchecked((int)Start.Date.Year * 366 + Start.Date.DayOfYear + (End.Date - Start.Date).Days);
}