Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

Enumerable.First: practical reference

Enumerable.First: practical reference: Immediately returns the first element or first predicate match and ends enumeration.

Version: .NET LINQ; verified on .NET 10.0.14

Syntax

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

Immediately returns the first element or first predicate match and ends enumeration.

Use it when absence is a contract violation that should be reported as an error.

Without parsing the message, which must not drive program logic, the exception does not distinguish an empty source from no predicate match.

No result throws InvalidOperationException. A null source or predicate causes ArgumentNullException.

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

Examples

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

Console.WriteLine(new[] { 4, 7, 8 }.First(value => value % 2 == 0));

Result

4

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

Sources