Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

reductions and statistics: practical reference

reductions and statistics: practical reference: A reduction folds a sequence into a statistic such as sum, mean, minimum, or histogram.

Version: C# language and .NET 10 reference runtime

Syntax

// reductions statistics

A reduction folds a sequence into a statistic such as sum, mean, minimum, or histogram.

Choose an accumulator type and numerically stable algorithm for the data scale.

A mean computed from a sum can overflow, and floating point accumulates error.

Empty data, null, NaN, and one observation require an explicit result definition.

The complete program actually uses reductions and statistics; its exact expected stdout is sum=12,avg=4.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

var values = new[] { 2, 4, 6 }; Console.WriteLine($"sum={values.Sum()},avg={values.Average()}");

Result

sum=12,avg=4

The complete program actually uses `reductions and statistics`; its exact expected stdout is `sum=12,avg=4`.

Sources