Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

Enumerable.Sum: practical reference

Enumerable.Sum: practical reference: Immediately enumerates and adds values in the overload-specific type; nullable overloads ignore null.

Version: .NET LINQ; verified on .NET 10.0.14

Syntax

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

Immediately enumerates and adds values in the overload-specific type; nullable overloads ignore null.

Use it for numeric totals when overflow rules and result type suit the domain.

Integer overloads can throw OverflowException. float and double retain rounding error and can yield NaN or infinity.

An empty source returns zero; an empty nullable source also returns nullable zero, not null.

The complete program actually uses Enumerable.Sum; its exact expected stdout is 9.

Examples

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

Console.WriteLine(new[] { 2, 3, 4 }.Sum());

Result

9

The complete program actually uses `Enumerable.Sum`; its exact expected stdout is `9`.

Sources