
String: When we create a string then this string creates new object of string type in memory. Every time string class take memory. It can’t reuse of memory for every string.
Stringbuilder: If you create Stringbuilder object then it takes memory in only one time. You can use this Stringbuilder object every time but it can’t take again memory. You can perform insert, replace or append without creating new instance for every time. Given bellow the example of this code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using testForClass1;
namespace testFor
{
class Program
{
static void Main(string[] args)
{
//string example
string name = "Farhan Sakib Jesy";
name = "Reza Karim";
Console.WriteLine("My Name Is: "+name);
//StringBuilder example
StringBuilder sb = new StringBuilder();
sb.Append("Farhan Sakib Jesy"+", ");
sb.Append("Reza Karim");
Console.WriteLine("My Name Is: " + sb);
Console.Read();
}
}
}
We can see string replace the name value. If we insert this two people name then we create a new string and it takes space on memory. But Stringbuilder class object can’t take any space in memory but also insert and append the string in same place. It can’t take any space in memory.