July 13, 2012

Immutable Objects

Immutable means "can not change". When an object is referred to as Immutable it means the internals cannot be changed i.e. it's member variables.

This is a design choice, where you might wish objects to be passed around as method arguments but you do not want the original object's internal values to change.
Remember that in C# when you pass an object of type Reference Type into a method as an argument, you are actually passing it by reference, meaning the method can change the original objects values.
To protect objects  from this make them immutable.

Objects are made immutable by simply making their internal variable readonly.