What is the Difference between String and StringBuilder in C# ASP.NET?

Here I am talking about ,What is the Difference between String and StringBuilder in C# ASP.NET? . Here below I am giving some core difference which will help you to understand what exactly String does and what StringBuilder does.And we have to identify correctly “what has to use “, that is more important! .

String:

1) String is immutable
2) String will create new instance every time so that the performance will be very slow.
3) During string concatenation, additional memory will be allocated.
4) We don’t have any append keyword in string.
5) String belongs System Namespace.

String builder

1) StringBuilder is mutable.
2) StringBuilder will use same instance of object to perform any action so that the performance will be high while camper with String.
3) During string concatenation, additional memory will be allocated if and only if the string buffer’s capacity is reached
4) we can use append keyword along with StringBuilder.
5) Stringbuilder belongs to System.Text namespace.



class Program
{
static void Main(string[] args)
{
//for example:
string dotnetstock = "DotnetStock "; //Creates a new object when we concatenate any other words along with dotnetstock variable it does not actually modify the dotnetstock variable, instead it creates a whole new string.
dotnetstock = dotnetstock + " .com ";
Console.WriteLine(dotnetstock);
StringBuilder SB = new StringBuilder("New");
SB.Append(" post");
Console.WriteLine(SB);
Console.Read();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *