C# Enumerable Sum(IEnumerable)

Description

Computes the sum of a sequence of Int32 values.

Syntax


public static int Sum(
  this IEnumerable<int> source
)

Parameters

  • source - A sequence of Int32 values to calculate the sum of.

Returns

returns The sum of the values in the sequence.

Example

The following code example demonstrates how to use Sum(IEnumerable) to sum the values of a sequence.


/*from   ww w.j a  va 2  s . c  o  m*/
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  

    List<float> numbers = new List<float> { 43.8F, 1.2F, 5.7F, 6.5F };

    float sum = numbers.Sum();

    Console.WriteLine("The sum of the numbers is {0}.", sum);

  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Linq »




Enumerable