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.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var