Keep in mind that types with a null value (both types with null values ββand reference types) behave differently for types of values ββthat are not null, in the general case when it comes to Min : null considered as "absent" in general, therefore it is unreasonable for the result of "a minimum of only missing values" to be a "missing value".
For instance:
int?[] nullableInts = new int?[5]; // All values null int? min = nullableInts.Min(); // No exception, min is null nullableInts[3] = 2; min = nullableInts.Min(); // No exception, min is 2
For types with null values, there really is no way to specify a "missing value" (unless the return type has been changed so that there is always a type with a null value), so the exception ... but this is quite simple in any case, as for a type with zero value, the only situation where there is no minimum is that the source is empty.
(It is possible that MinBy should behave the same :)
This is also consistent with conversions to LINQ to XML:
XElement element = null; int? x = (int?) element;
In principle, this makes a certain sense - not a single option will fit all.
source share