Jeśli chcesz wartość null
być traktowane jako default(DateTime)
mógłby zrobić coś takiego:
public class NullableDateTimeComparer : IComparer<DateTime?>
{
public int Compare(DateTime? x, DateTime? y)
{
return x.GetValueOrDefault().CompareTo(y.GetValueOrDefault());
}
}
i używać go w ten sposób
var myComparer = new NullableDateTimeComparer();
myComparer.Compare(left, right);
Innym sposobem na to byłoby uczynić metodę rozszerzenia dla Nullable
typów, których wartości są porównywalne
public static class NullableComparableExtensions
{
public static int CompareTo<T>(this T? left, T? right)
where T : struct, IComparable<T>
{
return left.GetValueOrDefault().CompareTo(right.GetValueOrDefault());
}
}
Jeżeli chcesz używać go tak jak to
DateTime? left = null, right = DateTime.Now;
left.CompareTo(right);
Czy zerowy zmbq
W jaki sposób wyniki różnią się od oczekiwanych, gdy używasz 'birthDate> hireDate', ponieważ jest to najłatwiejszy sposób na zrobienie tego. C# wprowadził ** podnoszone operatory **, aby ułatwić Ci pracę. –