Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 60 - C# Tutorial - difference between System.String and System.Text.StringBuilder

Strings of type StringBuilder are mutable where as strings of type System.String are immutable.  As StringBuilder objects are mutable, they offer better performance than string objects of type System.String, when heavy string manipulation is involved.

Part 60 - C# Tutorial - difference between System.String and System.Text.StringBuilder



Let's understand the meaning of mutable and immutable strings with an example. 
using System;
public class MainClass
{
    public static void Main()
    {
        string userString = "C#";
        userString += " Video";
        userString += " Tutorial";
        userString += " for";
        userString += " beginners";
        Console.WriteLine(userString);
    }
}


In this example, userString variable is changed 5 times.
1. C#
2. C# => C# Video
3. C# Video => C# Video Tutorial
4. C# Video Tutorial => C# Video Tutorial for
5. C# Video Tutorial for => C# Video Tutorial for beginners


Since, userString variable is of type System.String, and when we change this string 5 times, we end up with 5 string objects on the heap as shown in the diagram below. Immutable means, once a string object is created it cannot be changed, without creating another new string object. So in our example, When we initialize userString variable to "C#" we get one immutable string object on the heap. When we concatenate " Video" word to userString variable, the first created "C#" string object is orphaned(userString variable no longer points to this object). Now another new string object with words "C# Video" will be created to which the userString variable points to. So this process continues until, userString reference variable, points to the last string object (C# Video Tutorial for beginners), leaving the other 4 string onbjects on the heap(orphaned), until they are garbage collected, increasing the pressure on memory.




But on the other hand, StringBuilder string objects are mutable, meaning they can be changed inplace, without the need of creating another new StringBuilder object. The above example is rewritten using StringBuilder object.
using System;
using System.Text;
namespace Pragim
{
    public class MainClass
    {
        public static void Main()
        {
            StringBuilder userStringBuilder = 
                new StringBuilder("C#");
            userStringBuilder.Append(" Video");
            userStringBuilder.Append(" Tutorial");
            userStringBuilder.Append(" for");
            userStringBuilder.Append(" beginners");
            Console.WriteLine(userStringBuilder.ToString());
        }
    }
}


With StringBuilder, no matter how many times you manipulate a string, you will ever have only one instance. 




So in brief, here are the differences between String and StringBuilderobjects.
1. Objects of type StringBuilder are mutable where as objects of type System.String are immutable. 
2. As StringBuilder objects are mutable, they offer better performance than string objects of type System.String.
3. StringBuilder class is present in System.Text namespace where String class is present in System namespace.


Just imagine, the number of orphaned string objects that get created on the heap when you have a program as shown below.
using System;
namespace Pragim
{
    public class MainClass
    {
        public static void Main()
        {
            string strNumbers = string.Empty;
            for (int i = 0; i < 1000; i++)
            {
                strNumbers += i.ToString() + " ";
            }
            Console.WriteLine(strNumbers);
        }
    }
}

8 comments:

  1. Because the video was very helpful. I was able to finish the 60 video series without getting bored.
    Thank you Venkat. You did great job.

    God bless you

    Daryoes, USA-Virgina

    ReplyDelete
    Replies
    1. Yes very much excellent thx a lot Venkat , Bangalore-India

      Delete
    2. Hi Daryoes and Prasad,

      Thank you very much for taking time to give feedback. I am really glad you found these videos useful.

      I have organised all the ASP .NET, C#, and SQL Server video tutorials in to playlists, which could be useful to you.
      http://www.youtube.com/user/kudvenkat/videos?view=1&flow=grid

      Tips to effectively use my youtube channel.
      http://www.youtube.com/watch?v=nT9uF09RMkw

      If you want to receive email alerts, when new videos are uploaded, please feel free to subscribe to my youtube channel.
      http://youtube.com/kudvenkat

      If you like these videos, please click on the THUMBS UP button below the video.

      May I ask you for a favour. I want these tutorials to be helpful for as many people as possible. Please free to share the link with your friends and family who you think would also benefit from them.

      Good Luck
      Venkat

      Delete
    3. Hi Venkat,
      I learned JavaScript, jQuery and MVC by watching your video tutorials. You are doing great job. Hats off to you. I already share links to my friends and colleagues. Thay are also finding your tutorials very useful. I am requesting you to please add common Interview Q & A videos on MVC, AngularJS etc.
      Thanks in advance.

      Delete
  2. Thanks a lot for these super .net videos.... May Lord Venkateshwara Bless you..

    ReplyDelete
  3. hi, Due to ur videos last year I got a job in hcl and now iam preparing for capgemini interview ... again referencing ur videos only.. that's the greatness of ur videos.... thanks a lot..

    ReplyDelete
  4. Please make video on android.If needed make paid tutorials

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.