C# Enumerable Average(IEnumerable, Func)

Description

Computes the average of a sequence of Int32 values that are obtained by invoking a transform function on each element of the input sequence.

Syntax


public static double Average<TSource>(
  this IEnumerable<TSource> source,
  Func<TSource, int> selector
)

Parameters

  • TSource - The type of the elements of source.
  • source - A sequence of values to calculate the average of.
  • selector - A transform function to apply to each element.

Returns

returns The average of the sequence of values.

Example

The following code example demonstrates how to use Average(IEnumerable, Func) to calculate an average.


/* w w w . java 2 s  . co  m*/
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  
    string[] fruits = { "apple", "banana", "mango", "XML", "Java", "C#" };
    double average = fruits.Average(s => s.Length);
    Console.WriteLine("The average string length is {0}.", average);

  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Linq »




Enumerable