Difference between String and StringBuilder in C#

Difference between String and StringBuilder in C#

Hello Readers,

With the help of this post, I will try to explain the difference between String and StringBuilder in c#. It’s a common interview questions for c#, asp.net developers as well.

Difference between String and StringBuilder in C# Click To Tweet

String

String is immutable. Which means once we create string object we cannot modify it.  When we perform any operation on strings like insert, replace or append to change the string, it will discard the old value and a new instance will be created in memory to hold the new value.

Code Snippet

StringBuilder

StringBuilder is mutable. Which means once we create string builder object we can perform any operation like insert, replace or append without new instances getting created every time.

Code Snippet

The Differences – String vs StringBuilder

I have compiled the list of differences between String and StringBuilder in c# as below:

String StringBuilder

It is an immutable (unchanging)

It is mutable (liable to change)

Affects performance because every time it will create new instance

Boosts performance as it will use same instance of object to perform any action

You cannot use append keyword on strings

You can use append keyword on stringbuilder object

It belongs to System namespace

It belongs to System.Text namespace

Hope you like the post and got the difference between String and StringBuilder in c#.

What do you think?

Dear Reader,
If you have any questions or suggestions please feel free to email us or put your thoughts as comments below. We would love to hear from you. If you found this post or article useful then please share along with your friends and help them to learn.

Happy Coding!

  • Rahmat Ullah Afridi

    when we use the StringBuilder

    • When it comes to performance and much string manipulations are involved.