Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

Enumerable.Average: practical reference

Enumerable.Average: practical reference: Immediately enumerates numeric values and returns the mean in the overload-specific result type. Nullable overloads ignore null values.

Version: .NET LINQ; verified on .NET 10.0.14

Syntax

source.Average(/* overload-specific arguments */)

Immediately enumerates numeric values and returns the mean in the overload-specific result type. Nullable overloads ignore null values.

Use it for an arithmetic mean when the type and empty-set rules match the selected overload contract.

For int and long the result is double; for decimal it remains decimal. Accumulation can overflow according to the overload implementation.

An empty non-nullable source throws InvalidOperationException; an empty or all-null nullable source returns null.

The complete program actually uses Enumerable.Average; its exact expected stdout is 4.

Examples

using System;
using System.Collections.Generic;
using System.Linq;

Console.WriteLine(new[] { 2, 4, 6 }.Average());

Result

4

The complete program actually uses `Enumerable.Average`; its exact expected stdout is `4`.

Sources