What is C# mutable string and immutable string?

C# mutable string and immutable string is, in fact very simple concept and  you will understand with example.  The immutable meaning is unchanging over time or unable to be changed. The mutable meaning is changeable or modifiable.

Immutable String in C#

Immutable string cannot be changed once declared and defined. In a simple word, if we declare a string for example, string str = “interview”, a memory will be allocated for it and “interview” string will be placed in the memory.

If we try to change the string pointed by str variable e.g. str = “ interview sansar”, an another new memory will be created and “interview sansar” string will be placed in it. Now, str  variable will no longer point the memory where string “interview” was placed and old memory will be destroyed by GC.

Immutable string – C# program example:

class ImmutableString
    {
        static void Main(string[] args)
        {
            //a memory is allocated and "interview"
            //string is placed.
            String str = "interview";

            //On appending string, str will point to
            // new memory having string "interview sansar"
            // and no longer point to older memory.
            
            str = str + "sansar"; //output : interview sansar
            Console.Write(str);

            //if we assign a string to str, another memory will
            //be created and string will be written and GC
            //Will collect earlier one.
            str = "interviewsansar.com";
            
        }      

    }

You might be thinking that if we append a string or assign a string to the variable str, then another memory is getting created because of immutable properties of the string and older memory is getting collected by GC and destroyed, then what is the advantage of immutable string in C# language.

Read another interview question – What is benefits of immutable string in CSharp?

C# mutable string

C# mutable string means a string in a memory can be changed or modified and no new memory will be created on appending a string. We can use string builder class to create a mutable strings in c# programming.

C# Code Example – Mutable string using StringBuilder class

class MutableString
    {
        static void Main(string[] args)
        {
            //String builder will create a memory and place
            //the string into memory.

            //When we append the string same memory will be modified.
            StringBuilder str = new StringBuilder("interview");
            str.Append("sansar");
            str.Append(".com");
            Console.WriteLine(str);
            //Output: interviewsansar.com           
            
        }      
    }

Conclusion: Difference between immutable and mutable string in C# language is that, immutable string cannot be modified and mutable string can be modified at the memory location where the string is written.

NOTES:

If in a C# programming, a heavy manipulation of string is happening, specially within a loop, then it is better to use mutable string for better performance. Practically, I have experienced an application hang. So, we changed it to mutable string using string Builder class.

Related Posts