Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out if a class is immutable in C#?

How do I find out if a class is immutable in C#?

like image 270
StackUnderflow Avatar asked Jan 27 '09 00:01

StackUnderflow


People also ask

How do you know if a class is immutable?

It's properly constructed ( this reference doesn't leak from constructor) Object state cannot be modified (so class should not have setters and mutator methods) Don't store external (passed to constructor) references to mutable objects (e.g. create defensive copy of passed collection argument)

What is immutable in C?

In object-oriented and functional programming, an immutable object (unchangeable object) is an object whose state cannot be modified after it is created. This is in contrast to a mutable object (changeable object), which can be modified after it is created.

What is mutable and immutable in C?

Mutable and immutable are English words meaning "can change" and "cannot change" respectively. The meaning of the words is the same in the IT context; i.e. a mutable string can be changed, and. an immutable string cannot be changed.

What is an immutable class C#?

Technically, Immutable Class means a class which once created, its state cannot be changed later by any means internally or externally. Metaphorically, it is like a ceramic pot. We can give it any shape only in the beginning when we start making it.


2 Answers

There is ImmutableObjectAttribute, but this is rarely used and poorly supported - and of course not enforced (you could mark a mutable object with [ImmutableObject(true)]. AFAIK, the only thing this this affects is the way the IDE handles attributes (i.e. to show / not-show the named properties options).

In reality, you would have to check the FieldInfo.IsInitOnly, but this only applies to truly 100% immutable types (assuming no reflection abuse, etc); it doesn't help with popsicle immutability, nor things that are immutable in practice, but not in their implementation; i.e. they can't be made to be publicly mutable, but in theory the object supports it.

A classic example here would be string... everyone "knows" that string is immutable... of course, StringBuilder does mutate a string under the bonnet. No, seriously...

It is so hard to define immutability given this, let alone robustly detect it...

like image 144
Marc Gravell Avatar answered Sep 23 '22 06:09

Marc Gravell


Part of the problem is that "immutable" can have multiple meanings. Take, for example, ReadOnlyCollection<T>.

We tend to consider it to be immutable. But what if it's a ReadOnlyCollection<SomethingChangeable>? Also, since it's really just a wrapper around an IList I pass in to the constructor, what if I change the original IList?

A good approach might be to create an attribute with a name like ReadOnlyAttribute and mark classes you consider to be read-only with it. For classes you don't control, you can also maintain a list of known types that you consider to be immutable.

EDIT: For some good examples of different types of immutability, read this series of postings by Eric Lippert: http://blogs.msdn.com/ericlippert/archive/2007/11/13/immutability-in-c-part-one-kinds-of-immutability.aspx

like image 25
Neil Avatar answered Sep 20 '22 06:09

Neil