Friday, July 16, 2010

.NET Immutable vs. Mutable objects

Mutable: Changeable
Immutable: Unchangeable

Immutable objects

  • Remain constant after they are created.

  • Their internal state always remain valid

  • They are thread safe and multiple readers can read them simultaneously.

  • They are perfect for atomic, immutable value types e.g. a person's address

  • We can define the members of such class using the "readonly" keyword

  • To construct an immutable object we have to either provide a proper .ctor or a factory method

No comments:

Post a Comment