Use Action delegate to create Anonymous Methods for calculating the mean value in CSharp
Description
The following code shows how to use Action delegate to create Anonymous Methods for calculating the mean value.
Example
//w w w .j av a 2s . c o m
using System;
using System.Collections.Generic;
using System.ComponentModel;
class MainClass
{
static void Main()
{
Action<IList<double>> printMean = delegate(IList<double> numbers)
{
double total = 0;
foreach (double value in numbers)
{
total += value;
}
Console.WriteLine(total / numbers.Count);
};
double[] samples = { 1.5, 2.5, 3, 4.5 };
printMean(samples);
}
}
The code above generates the following result.