What is the Difference Between String and Stringbuilder in C#.

In this article, we will explain differences between String and StringBuilder in C# using asp.net coding example.

Here the number of differences are listed below.

Mutability: String is immutable (after creation, it can’t be modified).

For example:

String str = “Technical Diary";
str = str + “Most emerging blog in United State";

We can see in above example value of str variable has been changed from “Technical Diary” to “Technical Diary Most emerging blog in United State”. Because of this, it looks like String is mutable(modifiable) but the truth is that it is immutable(once created, it can’t be modified). Actually, in the first statement an object is created using the string literal “Technical Diary” and in the second statement when we assigned the new string literal (“Most emerging blog in United State”) to the variable str. In this case, the object itself didn’t change because a new object was created using the new string literal (“Technical Diary Most emerging blog in United State”). And the reference to it(new object) is assigned to the variable str. So, both of the string literals or objects exist with different references.

Mutability: StringBuilder is mutable(after creation, it can be modified),

For Example:

StringBuilder str = new StringBuilder(“Technical Diary ”);
Str.Append(“ : First Choice of Microsoft users”);

In the above example, we can see that in the first statement an object is created using the string literal “Technical Diary”. In the second statement, the value of the object is changed from “Technical Diary” to “Technical Diary: First Choice of Microsoft users” after concatenation. So here the object was modified instead of creating a new object like String.

Read Also: Difference between Angular 1 VS Angular 2

Performance:

Performance: StringBuilder is preferred over String for doing concatenation of strings because it is faster. In the case of String, when you concatenate strings, you are actually creating a new object every time since it is immutable and that makes it slower than StringBuilder.

String and StringBuilder Performance test:
using System;
using System.Text;

namespace StringVsStringBuilder
{
    class Program
    {
        static void Main(string[] args)
        {
            long startTime = DateTime.Now.Ticks;
            StringConcat();
            Console.WriteLine("Time taken for StringConcat: " + (DateTime.Now.Ticks - startTime)/ (decimal)TimeSpan.TicksPerMillisecond + "ms");
            startTime = DateTime.Now.Ticks;
            StringBuilderConcat();
            Console.WriteLine("Time taken for StringBuilderConcat: " + (DateTime.Now.Ticks - startTime)/ (decimal)TimeSpan.TicksPerMillisecond + "ms");
        }

        public static String StringConcat()
        {
            String str = "Technical Diary";
            for (int i = 0; i < 10000; i++)
            {
                str = str + "S Sharif";
            }
            return str;
        }
        public static String StringBuilderConcat()
        {
            StringBuilder strBuilder = new StringBuilder("Technical Diary");
            for (int i = 0; i < 10000; i++)
            {
                strBuilder.Append("S Sharif");
            }
            return strBuilder.ToString();
        }
    }
}

Output:

String and StringBuilder Performance test

In the above image, you can see that time taken by String is 383.344ms. And for the same operation time taken by StringBuilder is 1.0009ms. So, it proves that StringBuilder is faster than String.
This is from my end. Please mention in the comments in case you have any doubts related to the post: Difference between String and StringBuilder. Happy Coding 🙂

 

S Sharif

Founder of Technical Diary. Software engineer and passionate about technologies to learn and share. Crazy about best practices and new technology. I have worked in C#,ASP.NET MVC, HTML5, AngularJs SharePoint and SQL Server. I always try to keep myself updated and learn and implement new technologies. You can connect me on Twitter Facebook LinkedIn.

You may also like...

3 Responses

  1. Rozer says:

    Hi S Sharif,
    You explain in great way but some typing mistakes are there. Please correct it.

  2. Sanjay says:

    Hello can anyone tell why this program is not compiling?

    #include

    int main()
    {
    char a[] = “India”;
    char *p = “BIX”;
    a = “BIX”;
    p = “India”;
    printf(“%s %s\n”, a, p);
    return 0;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *