.NET Weekly Newsletter

.NET Weekly Newsletter

Share this post

.NET Weekly Newsletter
.NET Weekly Newsletter
StringBuilder vs String

StringBuilder vs String

Lets look at the difference between StringBuilder and String in C#

Muhammad Waseem's avatar
Muhammad Waseem
Dec 08, 2022
1

Share this post

.NET Weekly Newsletter
.NET Weekly Newsletter
StringBuilder vs String
1
Share

💡𝐒𝐭𝐫𝐢𝐧𝐠𝐁𝐮𝐢𝐥𝐝𝐞𝐫 𝐯𝐬 𝐬𝐭𝐫𝐢𝐧𝐠 𝐢𝐧 𝐂#

Difference b/w String and StringBuilder

𝐒𝐭𝐫𝐢𝐧𝐠𝐁𝐮𝐢𝐥𝐝𝐞𝐫
1. StringBuilder is mutable

2. StringBuilder will only create one object on heap and every time it would be updated with new value even if you append/insert 1 million values.

𝐒𝐭𝐫𝐢𝐧𝐠
1. String is immutable.

2. Every time when we update data in string it creates a new instance of object. So, if you update value 1K times it will create 1K new instances.

𝐓𝐢𝐦𝐞 𝐝𝐢𝐟𝐟𝐞𝐫𝐞𝐧𝐜𝐞 𝐨𝐯𝐞𝐫 𝟏𝟎,𝟎𝟎𝟎 𝐢𝐭𝐞𝐫𝐚𝐭𝐢𝐨𝐧𝐬
A good rule of thumb is to use strings when you aren't going to perform operations like(Append/Remove) repetitively, use StringBuilder for vice versa.

I took 10,000 iterations and checked the difference for that see the difference in picture.⏬

1

Share this post

.NET Weekly Newsletter
.NET Weekly Newsletter
StringBuilder vs String
1
Share
© 2025 Muhammad Waseem
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share