String and StringBuilder in C#

These two type of class use for character type of values but 'String' is Immutable and resides within System Namespace .  StringBuilder is mutable and it resides "System.Text" Namespace.
  • Mutable -->means -->change in value
  • Immutable -->means -->No change in value
I have full explained what is mutable and immutable with the help of  example which is given below:
using System;
using System.Text;
namespace String_stringbulider
    class Program
        public class student
            public void show(params String[] str)
                if (str.Length > 0)
                    for (int i = 0; i < str.Length; i++)
                    Console.WriteLine("No value found");
        static void Main(string[] args)
            String msg = "Hello";
            StringBuilder sb = new StringBuilder("Hey");
            String sms = msg.Insert(5,"Employee");


1.  Here First i have created a String variable msg and assigned a value "Hello".
Ex:  String msg = "Hello";
2. After that i have created a StringBuilder variable sb and assigned a value "Hey".
Ex: StringBuilder sb = new StringBuilder("Hey");
3. I have created a String variable sms again ,after that insert a value "Employee" in msg variable at fifth position (last of the Hello word) and assigned these value to the sms variable.
Ex: String sms = msg.Insert(5,"Employee");
4. After that i have inserted "student" in sb variable at third position(last of the Hey word).
Ex sb.Insert(3,"Student");
5. After that i have printed each variable values :-
  • msg variable printed only Hello.because value of msg variable did not change after the insert value Employee. So you can easily say that String are immutable .
  • When i have printed the value of sb then i have found that StringBuilder variable sb value changed after insert thC value student.So you can easily say that StringBuilder are mutable .
For More:-
I hope this is helpful for you.
To Get the Latest  Free Updates Subscribe
Click below for download whole application

1 comment:

Powered by Blogger.