Immutable collections C# with Example



Immutable collections C# with Example

The System.Collections.Immutable NuGet package provides immutable collection classes. 
Creating and adding items 
var stack = ImmutableStack.Create(); 
var stack2 = stack.Push(1); // stack is still empty, stack2 contains 1 
var stack3 = stack.Push(2); //  stack2  still contains  only  one,  stack3  has 2,  1 
Creating using the builder 
Certain immutable collections have a Builder inner class that can be used to cheaply build large immutable 
instances: 
var builder = ImmutableList.CreateBuilder(); //  returns  ImmutableList.Builder 
builder.Add(1); 
builder.Add(2); 
var list = builder.ToImmutable(); 
Creating from an existing IEnumerable 
var numbers = Enumerable.Range(1, 5); 
var list = ImmutableList.CreateRange(numbers); 
List of all immutable collection types: 
System.Collections.Immutable.ImmutableArray 
System.Collections.Immutable.ImmutableDictionary 
System.Collections.Immutable.ImmutableHashSet 
System.Collections.Immutable.ImmutableList 
System.Collections.Immutable.ImmutableQueue 
System.Collections.Immutable.ImmutableSortedDictionary 
System.Collections.Immutable.ImmutableSortedSet 
System.Collections.Immutable.ImmutableStack 
 

Parameter Details 
arg or arg1 the (first) parameter of the method 
arg2 
the second parameter of the method 
arg3 the third parameter of the method 
arg4 the fourth parameter of the method 
T or T1 the type of the (first) parameter of the method 
T2 
the type of the second parameter of the method 
T3 the type of the third parameter of the method 
T4 the type of the fourth parameter of the method 
TResult 
the return type of the method 

0 Comment's

Comment Form

Submit Comment