Immutability in C#

For some reason, there's been a lot of buzz lately around immutability in C#. If you're interested in algorithms and data structures, it's a fascinating subject. Immutable objects, according to Patrick Smacchia, have the following advantages:

  • They simplify multithreaded programming.
  • They can be used as hashtable keys.
  • They simplify state comparison.

A good introduction to immutable types by Patrick Smacchia:
Immutable types: understand their benefits and use them

More on immutability usefulness:
Immutability, Purity, and Referential Transparency
Immutable types can copy the world… safely!

Luca Bolognese on implementing immutable value objects:
Creating an immutable value object in C# - Part I - Using a class
Creating an immutable value object in C# - Part II - Making the class better
Creating an immutable value object in C# - Part III - Using a struct
Creating an immutable value object in C# - Part IV - A class with a special value
Creating an immutable value object in C# - Part V - Using a library

Finally, Eric Lippert shows how to implement a few common data structures as immutable types:
Immutability in C# Part One: Kinds of Immutability
Immutability in C# Part Two: A Simple Immutable Stack
Immutability in C# Part Three: A Covariant Immutable Stack
Immutability in C# Part Four: An Immutable Queue
Immutability in C# Part Five: LOLZ! <- this is of course the must read of the series ;)
Immutability in C# Part Six: A Simple Binary Tree
Immutability in C# Part Seven: More on Binary Trees
Immutability in C# Part Eight: Even More On Binary Trees
Immutability in C# Part Nine: Academic? Plus my AVL tree implementation
Immutability in C# Part Ten: A double-ended queue
Immutability in C# Part Eleven: A working double-ended queue

2 Comments

  • Yes, Julien, you're absolutely right.

  • I have used F# for a while having used Haskell prior to that and the simplicity of concurrent programming is a joy and one that imperative languages like C# etc will want to emulate in the future given the fact that processors are scaling out in terms of cores.

    I guess we will have to wait and see but I would bet that the next version of C# will introduce more abstract language constructs - LINQ was the tip of the iceberg.

Comments have been disabled for this content.