IS STRING MUTABLE OR IMMUTABLE IN .NET?

Today in Engineer's World, we are discussing a very important topic of C# -String in very easy way. Posted By- +Manish Kumar Gautam +LIV WIRE  +ASP.NET SOLUTIONS

Click imaginationhunt.blogspot to see latest Blogs
 

What does Mutable means?

Mutable: Mutable means whose state can be changed after it is created. In simple terms to remind and understand the term you can relate it by word "Mutants". For ex- In X-Men, Jennifer Lawrence has the power to change itself from Mutant to Human being and back to mutant.




What does Immutable means?

Immutable: Immutable means whose state cannot be changed once it is created. For ex- A human being cannot change its state. He can wear a different type costume to look different but that doesn't mean his state has been changed.

Well, we are very much clear about the differences between these two terms.  
Now, put our focus on iterating whether strings are mutable or immutable?
Let's check it out...

Click imaginationhunt.blogspot to see latest Blogs

Example- Let's take a string and apply some operation on it. That would give us a conclusion whether strings are mutable or immutable.

Practical Implementation:

using System;

namespace AspnetSolutions
{
    class StringImmutableNotMutable
    {
        public static void Main()
        {
          string rootStr = "Imaginationhunt.blogspot";

          // Operations(O)
          string manipulateStr = rootStr.Replace('o', 'a')// O1. Applying Replace()

          // Results(R)
          Console.WriteLine("Original String = " + rootStr);  //R1. "rootStr" Before operation
          Console.WriteLine("Using Replace()= " + manipulateStr); //R2. replacing performed
          Console.WriteLine("Original String = " + rootStr);  //R3. ''rootStr" After operation

          // Operations(O)
          string concatenateStr = rootStr + ".com";           // O2. Applying Concatenation
           

          // Results(R)
          Console.WriteLine("After Concatenate = " + concatenateStr);//R4. concat performed
          Console.WriteLine("Original String = " + rootStr);//R5. "rootStr" After concatenation
        }
    }
}

Output


(Refer O1, O2, R1, R2, R3, R4, R5 from above code.)
Let's understand output of the code in Steps:

Click imaginationhunt.blogspot to see latest Blogs

Step1: We initialize and declare a string "rootStr" as "Imaginationhunt.blogspot".

Step2: We apply some operation on string "rootStr".

Operation (O1)- Here, we are using the String.Replace() method to replace the old character 'o' by new character 'a'. And storing the output in string variable "manipulateStr". Why we are storing it in string data type? Because the return type of String.Replace() method is String.

Operation (O2)- Here, we are using the '+' sign to concatenate/join the "rootStr" with a hard coded string literal ".com". And storing the output in string variable "concatenateStr". Because the root string and the string literal both are in string type so, the type is string.  

Step3: Now, we first print the original string "rootStr", which give us -> "Imaginationhunt.blogspot" see (R1).

Step4: We now print the result for "manipulateStr", which give us -> "Imaginatianhunt.blagspat" see (R2). As we can see the Replace() method is performing on "rootStr", which means the string now has been changed.

Click imaginationhunt.blogspot to see latest Blogs

Step5: Let just pass the string "rootStr" to overload of WriteLine() to see whether it's showing the original string "rootStr" (i.e. "Imaginationhunt.blogspot") or the "manipulateStr" (i.e. "Imaginatianhunt.blagspat") see (R3). And what we got as result, the "rootStr", which means whatever operation we may perform on "rootStr". At the end we are always getting our original string, which is our root string. This shows the immutable nature of string.

Step6: Let's perform another operation on "rootStr" to make it more clear and understandable. This time we concatenate our original string with string literal ".com" see (O2).

Step7: Now, printing the string "concatenateStr" see (R4). Returns a new string instance instead of changing the old one.

Step8: And finally, printing again the "rootStr". And it gives us the exact string as the original string see (R5).

Therefore, from both the operation that we perform on string and getting back our original string shows the immutable nature of string
Hence, we concluded that the strings in .Net are immutable.


To read STRINGS (NULL AND EMPTY) IN C# - Click here.


Related Questions:

#newtoprgm try firstprogram

Q-1 What does the term 'immutable' means in terms of string objects?
A) We can modify characters included in the string
B) We cannot perform various operation of comparison, inserting, appending, etc.
C) We cannot modify characters contained in the string.
D) None of the mentioned
Ans- Option (C).
Explanation- String objects are 'immutable' it means we cannot modify the characters contained in string also operation on string produce a modified version rather than modifying characters of string.

Click imaginationhunt.blogspot to see latest Blogs

Keep learning and sharing...

Comments

Popular posts from this blog

SSC CGL 2016 Tier-1 Marks / Result

DATA TYPES IN C#

Pattern printing program

Top Mutual Funds

SSC CGL 2017 TIER 2 RESULT OUT

JUST IN TIME (JIT)

Can we have multiple Main methods in one class

TOP 15 VISUAL STUDIO SHORTCUT KEYS

Top 10 Interview Tips and Questions