C# ===> Immutable and Mutable

Immutable type are types whose instance data, fields and properties, does not change after the instance is created. Most value types are immutable, but the mutable type are A type whose instance data, fields and properties, can be changed after the instance is created. Most Reference Types are mutable.
All string objects are immutable in C#. Objects of the class string, once created, can never represent any value other than the one they were constructed with. All operations that seem to “change” a string instead produce a new one. This is inefficient with memory, but extremely useful with regard to being able to trust that a string won’t change out form under you- because as long as you don’t change your reference, the string being referred to will never change.
A mutable object, by contrast, has data fields that can be altered. One or more of its methods will change the contents of the object, or it has a Property that, when written into, will change the value of the object.
Mutable type, in C#, is a type of object whose data members, such as properties, data and fields, can be modified after its creation.Mutable types are used in parallel applications, where the objects of mutable value type are maintained in the stack by the Common Language Runtime (CLR). This provides some optimization, which makes it faster than heap-allocated objects. To prevent synchronization errors from occurring in parallel applications due to multiple threads accessing the shared data, mutable types are used with some kind of locking mechanism.Due to its inherent nature, mutable type data can get modified during run time, so mutable type is often used when the object contains a large amount of changeable data. Although mutable type is not as thread-safe and secure as immutable type, it is more often used with variables of value type, which get allocated in the stack, improving performance.

C# supports immutability thanks to 2 keywords: const and readonly. They are used by the C# compiler to ensure that the state of a field won’t be changed once an object is created. Why 2 keywords? Because the readonly keyword allows state modification within constructor(s) while the const keyword doesn’t. For example:

class Article {
Article(string name,int price)    {
m_Name = name; // <- Compilation error
m_Price = price;
}
const string m_Name = “Ballon”;
readonly int m_Price;
}

Immutable String

Immutable string can’t be alter, once we have assign a value to immutable object state can’t be changed. We can use System.String to use immutable string.   String str = “Technology Crowds”;  //*  Immutable String Example

string str = “Technology Crowds” + “Technology”;

Mutable String

We can assign multiple values to mutable string and object state can be altered.

Best example of Mutable is stringbuilder object can be used. We can multiple concatenation to mutable string. Mutable have very good flexibility to change object state, mutable string can generate long string.

We can use given below namespace to use mutable string in c#

using System.Text;   //*  namespace used to import string builder

StringBuilder strLong = null;   //*  [Mutable String] example

strLong.Append(“Technology”);

strLong.Append(“Crowds”);

 

So, strings are immutable. What this means in application is that modifying a string creates new string data on the heap and modifies the pointer on the stack to point to the new location. The data on the heap that is not referenced from the stack will be cleared at garbage collection time, but until then it’s wasteful.

In comparison, StringBuilders are mutable. Modifications/additions to the stringbuilder will update the data in the heap instead of making copies of it each time.

StringBuilder sb = new StringBuilder;

sb.Add(“test”);

sb.Add(” and test2″);

That code will create a pointer on the stack and a data value on the heap, update that data value to add the second string and that’s it. Doing the same with strings would have left an abandoned string data value on the heap of value “test” waiting on the GC.

Ints, and most other primitive types in C#, are immutable

Immutable type object means the object which cannot change it’s state once it is created

Immutable objects can be useful in multi-threaded applications. Multiple threads can act on data represented by immutable objects without concern of the data being changed by other threads. Immutable objects are therefore considered to be more thread-safe than mutable objects.

Best example for Immutable in C# is String Class.

C# Supports Immutability with two key words const and readonly. They are used by the C# compiler to ensure that the state of a field won’t be changed once an object is created.

Mutable Objects:

Mutable Objects are complety opposite to immutable objects where we can modified any where.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s