Search code examples
c#.netimmutability

Immutable collections?


I am making most of my basic types in my app, immutable. But should the collections be immutable too? To me, this seems like a huge overhead unless I am missing something.

I am talking about collections to hold Point3 values, etc which can be added as it goes at different times. So if there are 1M values in a collection, and you needed to delete 1 of them, you would have to create the same collection all over again, right?


Solution

  • Eric Lippert has a series on Immutability in C#, and if you read it all the way through he implements a couple different immutable collections:

    1. Immutability in C# Part One: Kinds of Immutability
    2. Immutability in C# Part Two: A Simple Immutable Stack
    3. Immutability in C# Part Three: A Covariant Immutable Stack
    4. Immutability in C# Part Four: An Immutable Queue
    5. Immutability in C# Part Five: LOLZ!
    6. Immutability in C# Part Six: A Simple Binary Tree
    7. Immutability in C# Part Seven: More on Binary Trees
    8. Immutability in C# Part Eight: Even More On Binary Trees
    9. Immutability in C# Part Nine: Academic? Plus my AVL tree implementation
    10. Immutability in C# Part Ten: A double-ended queue
    11. Immutability in C# Part Eleven: A working double-ended queue