C# and .Net Interview Questions”- Demonstrate why String Builders are faster than strings?

Strings: – strings are also called as immutable because every time you store a new value in string a new copy is created in the memory location.

String builders: – string builders are also called as mutable because
every time you store a new value in string builder it actually appends the value in the memory location. This means insertion is done on existing string.

Let’s demonstrate a small example which will give a clear idea on who is faster string builder or string.

This is very simple three steps procedure as follows.

Step1: –

Create a new console application like below diagrams.

Go to file > New > Project > Console Application and Click Ok.

Step2: –

Import System.Text namespace in your application.


Step3: –

Write the below code snippet in your main class.

staticvoid Main(string[] args)
while (true)
Console.WriteLine("*********Begin Test*********");
Console.WriteLine("Creating string");
Console.WriteLine();//for having a gap in the lines.
List<string>strlist = newList<string>();
for (int i = 0; i < 30000; i++)
Console.WriteLine("Time taken by string to concate....");
DateTime start = DateTime.Now;
stringConcateString = string.Empty;
foreach (strings instrlist)
ConcateString += s + "";
TimeSpan time = (DateTime.Now - start);
Console.WriteLine("Total Time : {0} MilliSeconds", time);
Console.WriteLine("Creating StringBuilder");
start = DateTime.Now;
StringBuildersb = newStringBuilder();
foreach (strings instrlist)
sb.Append(s + "");
time = (DateTime.Now - start);
Console.WriteLine("Time taken by StringBuilder to concate....");
Console.WriteLine("Total Time : {0} MilliSeconds", time);
Console.WriteLine("**********End Test**********");

Now run your application and you will see the result like below diagram.

In the above result you can see that the time taken by the string to concateis greater than the time taken by the string builder to concate, this indicates that the string builders performs faster than the strings.

For more information about generic, please watch the below video.

Please click here to see more C# interview questions and answers


Visit Authors blog for more Most asked c# interview questions


About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s