Immutable collections

De Banane Atomic
Version datée du 8 septembre 2024 à 14:29 par Nicolas (discussion | contributions) (Page créée avec « Category:CSharp = Links = * [https://learn.microsoft.com/en-us/archive/msdn-magazine/2017/march/net-framework-immutable-collections Immutable Collections] * F# Collections * Record Nondestructive Mutation = Definition = An immutable collection is a collection of instances that * preserves its structure all the time: no addition or removal of elements, no change of order * disallows element-level assign... »)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
Aller à la navigationAller à la recherche

Links

Definition

An immutable collection is a collection of instances that

  • preserves its structure all the time: no addition or removal of elements, no change of order
  • disallows element-level assignments

While still offering APIs to perform mutations: Add, Remove, ...

The immutable collections are designed with two goals in mind:

  • reuse as much memory as possible, to avoid copying, and to reduce pressure on the garbage collector
  • support the same operations offered by mutable collections with competitive time complexities

Immutable Stacks

Cs.svg
var s1 = ImmutableStack<int>.Empty;

// call push, s1 remains unchanged, and it returns a reference to the resulting immutable stack which contains the element 1.
var s2 = s1.Push(1);

// call push, s2 remains unchanged, and it returns a reference to the resulting immutable stack which contains no element.
var s3 = s2.Pop();

Immutable Lists

Immutable Arrays

Immutable Dictionaries