Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

Enumerable.Where: practical reference

Enumerable.Where: practical reference: The deferred filter returns original elements for which the predicate is true; the indexed overload passes the source index.

Version: .NET LINQ; verified on .NET 10.0.14

Syntax

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

The deferred filter returns original elements for which the predicate is true; the indexed overload passes the source index.

Use it to select elements without changing their type or value.

The predicate runs again on every enumeration and can observe a changed source. Side effects make behavior hard to predict.

An empty source yields an empty result. A null source or predicate causes ArgumentNullException.

The complete program actually uses Enumerable.Where; its exact expected stdout is 2,4.

Examples

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

Console.WriteLine(string.Join(",", new[] { 1, 2, 3, 4 }.Where(value => value % 2 == 0)));

Result

2,4

The complete program actually uses `Enumerable.Where`; its exact expected stdout is `2,4`.

Sources