Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.Collections.Immutable.ImmutableList: practical reference

System.Collections.Immutable.ImmutableList: practical reference: ImmutableList<T> returns structurally shared list versions after update operations.

Version: .NET API; verified on .NET 10.0.14

Syntax

System.Collections.Immutable.ImmutableList /* select a documented member or constructor */

ImmutableList returns structurally shared list versions after update operations.

Use it when list operations are needed while previous versions must remain valid.

Its indexing and update costs differ from List, so immutability does not make it automatically faster.

An out-of-range index throws, and object elements referenced by the list remain mutable.

The complete program actually uses System.Collections.Immutable.ImmutableList; its exact expected stdout is 2.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

var list = ImmutableList<int>.Empty.Add(1).Add(2);
Console.WriteLine(list[1]);

Result

2

The complete program actually uses `System.Collections.Immutable.ImmutableList`; its exact expected stdout is `2`.

Sources