Why to use StringBuilder instead of string in C#?
Danish Farhaj
Lead Software Engineer @ F3 Technologies | Making Code Smarter, One Bug at a Time
From developer perspective both serves the same purpose that is saving / manipulating string values. Most of the time developers don't bother about memory management which is not a problem in case of small applications.
So go through following to article to understand the basic difference between string and string builder and why to use StringBuilder instead of String.
String?
A string is an immutable object. Immutable is when we create string objects in code so we cannot modify or change that object in any operations like insert new value, replace or append any value with the existing value in a string object. When we have to do some operations to change string simply it will dispose of the old value of string object and it will create a new instance in memory for hold the new value in a string object, for example:
Note
领英推荐
StringBuilder
System.Text.Stringbuilder is a mutable object which also holds the string value, mutable means once we create a System.Text.Stringbuilder object. We can use this object for any operation like insert value in an existing string with insert functions also replace or append without creating a new instance of System.Text.Stringbuilder for every time so it’s using the previous object. That way, it works fast compared to the System.String. Let’s see an example to understand System.Text.Stringbuilder.
Note
To understand string and stringbuilder more deeply you need to go through the link :
You can find more C# interview questions on the link below :