« Immutable collections » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
(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... »)
 
Aucun résumé des modifications
 
(2 versions intermédiaires par le même utilisateur non affichées)
Ligne 23 : Ligne 23 :


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


= Immutable Lists =
Other immutable collection types:
 
* Immutable Lists
 
* Immutable Arrays
= Immutable Arrays =
* Immutable Dictionaries
 
 
= Immutable Dictionaries =

Dernière version du 8 septembre 2024 à 14:34

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.
// s3 and s1 target the same reference: s3 == s1
var s3 = s2.Pop();

Other immutable collection types:

  • Immutable Lists
  • Immutable Arrays
  • Immutable Dictionaries