Simple Immutable POCO Types in C#

davepermen
1 min readApr 12, 2015

--

I wrote some simple POCO that’s immutable, and flexible in modification on the fly by constructing a new one.

C# 6 will make it a bit less verbose (but not that much yet ☹)

Some usage examples

Class used in the examples, and it’s implementation

The class has two constructors, one setting all values (and require to set all), and one using a previous class instance.

On the second constructor, all parameters are optional, except for the previous class. This, combined with named parameters, allows to just change the part of the state one wants.

Optional different syntax

One could consider, instead of the second constructor, a ‘Modify’ method (choose what ever name you like) that returns a new object.

For demo purposes, I’ve implemented Modify using the first constructor as an extension method. Usage below is quite nice to read, I think. I’m considering switching to that approach now (while writing the blogpost.. horray)

--

--