Can I safely use DateTime as a key in a SortedDictionary<K, V> without implementing my own IComparer?
I’ve tried it and sort order is maintained and importantly for my purpose the .ContainsKey<T> method works as expected. But I just want to double check before committing myself down this road.
you will not need a new IComparer. your default IComparer will use the
Equals()from your value object and compare/sort with that. Since the DateTime type implements theEquals()correctly, the default IComparer will sort as you want it to (like you have discovered).creating new comparer’s is more for complex collections that you may have written yourself. i wrote a complex comparer once. it’s a bit mind blowing.