Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

Enumerable.ToArray: practical reference

Enumerable.ToArray: practical reference: Immediately enumerates and copies elements into a new fixed-length array.

Version: .NET LINQ; verified on .NET 10.0.14

Syntax

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

Immediately enumerates and copies elements into a new fixed-length array.

Use it when a snapshot, indexing, and an exact Length are required.

The copy is shallow: element objects remain the same references. Materializing a large or infinite source consumes memory or never completes.

An empty source yields an Array.Empty-equivalent empty array; a null source causes ArgumentNullException.

The complete program actually uses Enumerable.ToArray; its exact expected stdout is Int32[]:3.

Examples

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

var array = Enumerable.Range(1, 3).ToArray();
Console.WriteLine($"{array.GetType().Name}:{array.Length}");

Result

Int32[]:3

The complete program actually uses `Enumerable.ToArray`; its exact expected stdout is `Int32[]:3`.

Sources