Q:

What is the difference between string and StringBuilder in c#?

0

What is the difference between string and StringBuilder in c#?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

String
The string is immutable. Immutable means once we create string object we cannot modify. Any operation like insert, replace or append happened to change string simply it will discard the old value and it will create a new instance in memory to hold the new value.
String belongs to System namespace

=>Example
string str = “hi”;
//It creates a new string instance instead of changing the old one
str += “help”;
str += “test”;

StringBuilder
String builder is mutable. Mutable means once we create string builder object we can perform any operation like insert, replace or append without creating new instance for every time.
StringBuilder belongs to System.Text namespace

=> Example
StringBuilder sb = new StringBuilder(“”);
//It does not creats new instance for every time.
sb.Append(“hi”);
sb.Append(“help “);
string str = sb.ToString();

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Top 100 C# interview questions and answers 2022

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
List out some of the exceptions in C#?... >>
<< What is the difference between dispose and finaliz...