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 Start;
public DateTime End;
public DateRange(DateTime start, DateTime end)
{
Start = start.Date;
End = end.Date;
}
public override int GetHashCode()
{
// ???
}
}
What’s the best way to implement GetHashCode so no two objects of a differing range will generate the same hash? I want hash collisions to be as unlikely as possible, though I understand Dictionary<> will still check the equality operator which I will also implement, but didn’t want to pollute the example code too much. Thanks!
You can use the method from Effective Java as Jon Skeet shows here. For your specific type: