C# interview question :- Why is stringbuilder concatenation more efficient than simple string concatenation?

C# interview question :- Why is
stringbuilder concatenation more efficient than simple string concatenation?


Answer:

This question is one of the favorites question which comes during
c#interviews and the interviewer is not only expecting which one of them is more efficient. But he is also expecting the reason for the same.

In order to understand the same better lets consider the below scenario where we have two lines of code which does string concantenation.

For a simple string concantenation code shown below it will create 3 copies of string in memory.

//The below line of code Creates one copy of the string
string str = "shiv";
/* The below line of  code creates three copies of string object one for
 the concatenation  at right hand side and the other for new value at the
 left hand side. The first old allocated memory is sent for garbage 
collection.*/
str = str + "shiv";

When you use the string builder for concatenation it will create only one copy of the object.

/* The below code uses string builder and only one 
object is created as compared to normal string where we have 3 copies created*/
StringBuilder objBuilder = new StringBuilder();
objBuilder.Append("shiv");

Ok now summarizing what should we say to the interviewer.

String is immutable. Immutable means once assigned it can not be changed. Thats why it creates more copies of the object as it can not use the same instance.String builder is mutable , in other words the same object will  changed rather than creating new objects.

So string builder is more efficient as compared to simple string
concatenation.

See my 300 important c# interview questions and answers

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s