Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immutable type and property in C#

What is meant by immutable type and immutable property in C# ? can you give simple example?

like image 784
Yodha Avatar asked Mar 29 '10 11:03

Yodha


People also ask

What is an immutable type?

An immutable type, in the context of C#, is a type of object whose data cannot be changed after its creation. An immutable type sets the property or state of the object as read only because it cannot be modified after it is assigned during initialization.

What immutable means in C?

An immutable object is defined as an object that cannot be changed after it has been created.

What is immutable value type?

With an immutable ref type, any change made to an existing reference produces a new object instead of change the existing one, so you get the value type's behavior that whatever value you're holding cannot be changed through some other name. Of course the System. String class is a prime example of such behavior.

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.


2 Answers

An immutable type is a type of which its properties can only be set at initialization. Once an object is created, nothing can be changed anymore. An immutable property is simply a read-only property.

In the following example, ImmutableType is an immutable type with one property Test. Test is a read-only property. It can only be set at construction.

class ImmutableType {     private readonly string _test;     public string Test     {         get { return _test; }     }      public ImmutableType(string test)     {         _test = test;     } } 

See also: The Wikipedia article, and some Stack Overflow questions on the topic.

like image 119
fretje Avatar answered Sep 30 '22 18:09

fretje


In addition to @fretje's answer above, in C#6 and later, getter-only auto properties have now been implemented, which allows for immutable auto-properties without the need for the additional explicit private readonly backing field. The equivalent code would be abbreviated to:

class ImmutableType {     public string Test     {         get; // No Set at all, not even a private set.     }      public ImmutableType(string test)     {         Test = test; // The compiler understands this and initializes the backing field     } } 

Note that private set only provides a restricted encapsulation of change to the property from within the same class, and thus isn't truly immutable:

public string Test {     get;     private set;  // Not immutable, since this is still mutable from within the class } 

More about immutability

As others have said, an immutable Property is a property which cannot change once it has been set. Setting of the 'only' value is done during construction.

An immutable Type is a type where all (externally visible) properties and fields are immutable - for example the "Record" Type originally scheduled for C#7 (hopefully now 8) would have been an immutable type. Other examples of Immutable Types are Tuples, and all anonymous classes.

Immutable fields should be qualified with the readonly keyword in C# - this is enforced by the compiler to ensure that no other code attempts to change the field outside of the constructor.

Wherever possible, immutability of fields, variables and properties is regarded as good practice, as this greatly reduces the surface area for bugs (since fields represent the state of an object, preventing change to fields reduces the number of states).

The benefit of immutability is especially important in multi-threaded programs, where two or more threads concurrently access the same object. Since multiple concurrent reading threads can safely read the value of a field or property, the programmer doesn't need to be concerned about thread safety issues relating to change to the field by other threads (because change to the property is prohibited)

One common drawback of immutability when dealing with complex objects consisting of multiple composed objects, is that the entire graph needs to be built 'in one go', which can lead to messy code. A common solution here is to use the Builder pattern as a scaffold, which allows a transient, mutable representation to be built in steps, and then the final, immutable object is obtained in the final .Build() step.

like image 34
StuartLC Avatar answered Sep 30 '22 16:09

StuartLC