generic ?

, integer, float double. , , . . , , , -

public static T SUM<T>(IEnumerable<T> dataCollection)
{
T total;
foreach(var value in dataCollection)
total += value;
return total;
}

      

, , ?

,

+3




2


, + / - / * / etc ...

, , . .

, , , +

T

. , lambda



public static T SUM<T>(IEnumerable<T> dataCollection, Func<T, T, T> sum)
{
  T total = default(T);
  foreach(var value in dataCollection)
    total = sum(total, value);
  return total;
}

List<int> list = ...;
list.Sum((left, right) -> left + right);

      

. , , , System.Linq.Enumerable.Aggregate

. , , , .

+6




,



# Plus

0









All Articles