💡𝐒𝐭𝐫𝐢𝐧𝐠𝐁𝐮𝐢𝐥𝐝𝐞𝐫 𝐯𝐬 𝐬𝐭𝐫𝐢𝐧𝐠 𝐢𝐧 𝐂#
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.⏬