What is the cause of an InvalidOperationException in Enumerable.Aggregate <TSource>?

Why is only one overload causing this exception?

A small update: I understand that the framework developers have made a design decision. The question really is, why was this decision made, is there an appropriate design template or something else? Because if I were developing this, I would return the default value (TSource). What is wrong with this approach?

+3
source share
3 answers

One overload that throws an exception takes arguments of type IEnumerable<TSource>( this) and Func<TSource, TSource, TSource>. It starts from the first value and accumulates from there. Without the first value (if sourceempty), there is no way for the function to know what to return.

The other two overloads accept a type argument TAccumulateto act as a seed. Even if sourceempty in this case, the function may simply return the initial value.


. , default(T) . - , , ( ) API. Aggregate , , , .

default(T) , :

  • ;
  • default(T)

, , Aggregate mode . , , ( 0 ).

, , :

Aggregate [1, 2, 3, ...] -> 1 * 1/2 * 1/3 * ...

, , , , .

+1

Aggregate<TSource>, , - Aggregate<TSource>(this IEnumerable<TSource> enumerable).

, ( ) . , . :

  • ,
  • default(TSource)

. , , , ( )

, , .

+4

, , . -, .

+2

Source: https://habr.com/ru/post/1739201/


All Articles