CKTIS / C#
Enumerable.Take: practical reference
Enumerable.Take: practical reference: The deferred operation returns at most the first count elements and ends enumeration when the limit is reached.
Version: .NET LINQ; verified on .NET 10.0.14
Syntax
source.Take(/* overload-specific arguments */)The deferred operation returns at most the first count elements and ends enumeration when the limit is reached.
Use it to limit work and take a prefix; with OrderBy it supports controlled top-N.
Without stable order, top-N can be nondeterministic. The source can produce side effects for the prefix read.
A negative or zero count yields an empty result; a count beyond the length returns the whole source.
The complete program actually uses Enumerable.Take; its exact expected stdout is 1,2.
Examples
using System;
using System.Collections.Generic;
using System.Linq;
Console.WriteLine(string.Join(",", new[] { 1, 2, 3, 4 }.Take(2)));
Result
1,2
The complete program actually uses `Enumerable.Take`; its exact expected stdout is `1,2`.