You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enumerable.Sum<Nullable<T>> has bad implementation #240
When Enumerable.Sum<Nullable<T>> is called with an IEnumerable<Nullable<T>> where all elements are null, the method currently returns 0.
Expected behavior
It is expected that Enumerable.Sum<Nullable<T>> should return null in this scenario, as there are no non-null values to sum. Returning 0 can be ambiguous, as 0 is also a valid sum for inputs like [0, 0].
Minimal repro code
// Test case 1: All nullsIEnumerable<int?>allNulls=newint?[]{null,null,null};int?sumAllNulls=allNulls.Sum();Console.WriteLine($"Sum of all nulls: {sumAllNulls}");// Currently outputs: Sum of all nulls: 0// Test case 2: Mixed nulls and zerosIEnumerable<int?>mixedNullsAndZeros=newint?[]{null,0,null,0};int?sumMixed=mixedNullsAndZeros.Sum();Console.WriteLine($"Sum of mixed nulls and zeros: {sumMixed}");// Currently outputs: Sum of mixed nulls and zeros: 0// Test case 3: All zerosIEnumerable<int?>allZeros=newint?[]{0,0,0};int?sumAllZeros=allZeros.Sum();Console.WriteLine($"Sum of all zeros: {sumAllZeros}");// Currently outputs: Sum of all zeros: 0
Activity