Questions tagged [immutable-collections]

Immutable Collections is a library containing immutable data structures created by Microsoft for .NET.

Immutable Collections is a library containing immutable data structures created by Microsoft for .NET.

Related tags

93 questions
97
votes
6 answers

Why use ImmutableList over ReadOnlyCollection?

.NET 4.5 has a new namespace System.Collections.Immutable This package provides collections that are thread safe and guaranteed to never change their contents, also known as immutable collections. I'm confused. Isn't the thread safety problem…
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
91
votes
9 answers

Is it better to return an ImmutableMap or a Map?

Let's say I'm writing a method that should return a Map. For instance: public Map foo() { return new HashMap(); } After thinking about it for a while, I've decided that there is no reason to modify this Map once…
AMT
  • 1,016
  • 1
  • 7
  • 13
56
votes
5 answers

What's the difference between a ReadOnlyDictionary and an ImmutableDictionary?

In C#, what is the key difference (in terms of features or use cases) between these two containers? There doesn't appear to be any information comparing these on…
matt_rule
  • 1,220
  • 1
  • 12
  • 23
47
votes
4 answers

Difference between ImmutableArray and ImmutableList

What is difference between ImmutableArray and ImmutableList, and where would it be best to use each?
user3391731
  • 473
  • 4
  • 6
24
votes
1 answer

Do I have to include all System.Collections.Immutable dependencies?

I just switched from (an older) Microsoft.Bcl.Immutable NuGet package to System.Collections.Immutable and was surprised to find all these new package dependencies in my…
enzi
  • 4,057
  • 3
  • 35
  • 53
17
votes
2 answers

What's the best pattern for passing Immutable Collections across APIs

Before immutability, IEnumerable was the go-to interface in many APIs since this had the advantage that the API was insensitive to the actual type of the passed object. public void DoSomeEnumerationsWithACollection(IEnumerable
NeilMacMullen
  • 3,339
  • 2
  • 20
  • 22
13
votes
3 answers

Should one prefer ImmutableDictionary, or ImmutableSortedDictionary?

I have heard that the .NET System.Collections.Immutable collections are implemented as balanced binary trees in order to satisfy their immutability constraints, even collections which traditionally model hash tables like Dictionary, by using the…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
12
votes
1 answer

ImmutableSortedDictionary range enumeration by key

I was reading about C#'s ImmutableSortedDictionary in System.Collections.Immutable and thinking about how to apply it in my program. I quite like C++'s lower_bound and upper_bound (see here), and I was rather expecting to see something of the sort…
J Trana
  • 2,150
  • 2
  • 20
  • 32
10
votes
2 answers

Nuget seems to install the wrong version of System.Collections.Immutable

I have a confusing issue with Nuget. I have a number of projects who claim to have System.Collections.Immutable installed at version 1.3.0 but if I look at the version of the dll in all the references I see version 1.2.1.0 When I open up the DLL…
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
9
votes
1 answer

Why is ImmutableList's enumerator so much slower compared to List

I have a piece of code that iterates over a small list frequently. Given that the list never changes during runtime, I replaced the implementation with ImmutableList. Looking at a performance trace by dotTrace, this performs much worse than a…
JohnD
  • 3,884
  • 1
  • 28
  • 40
8
votes
1 answer

How to get altered tree from Immutable tree, maximising reuse of nodes

I have a tree structure data like this: [{ id: 54, name:123, children: [{ id: 54, name:123, children: [{ id: 154, name:1234, children []... }] }] }, { ... }] I am…
8
votes
1 answer

C# multidimensional immutable array

I need to create a field for simple game. In first version the field was like Point[,] - two dimensional array. Now i need use System.Collections.Immutable (it's important condition). I trying to google and can't find anything, that can help me. I…
murzagurskiy
  • 1,273
  • 1
  • 20
  • 44
7
votes
4 answers

When immutable collections are preferable than concurrent

Recently read about immutable collections. They are recommended to be used as a thread safe for reading, when the read operations are performed more often than write. Then I want to test read performance ImmutableDictionary vs ConcurrentDictionary.…
xneg
  • 1,204
  • 15
  • 24
7
votes
7 answers

Create an ImmutableArray without copying

Is there any way (possibly a dirty hack) to create an ImmutableArray which will just use a specified array, instead of copying it over? I have an array which I know won't change, and I want to create an ImmutableArray to allow clients to access my…
Yair Halberstadt
  • 5,733
  • 28
  • 60
7
votes
3 answers

ImmutableList does not contain a constructor that takes 0 arguments

I'm trying to initialize an immutable list like a regular list, but it's telling me it doesn't take 0 arguments. It throws the same error if I pass 1 argument, 2 arguments, etc. public static readonly ImmutableList AddressTestCases = new…
Gyn Manstot
  • 183
  • 2
  • 9
1
2 3 4 5 6 7