C# - String vs StringBuilder


String Object

In C#, a string is a sequence of characters that is used to represents text. Strings are reference types, which means they are allocated on the heap and can be null. They are also called immutable, which means that once a string is created, its contents cannot be changed.

C# String vs String Builder

Here is an example of a string in C#:

string message = "TutorialsTrend!";

String Builder Object

C# introduced the StringBuilder in the System.Text namespace. In C#, a stringbuilder is also a sequence of characters that is used to represents text. Strings are reference types, which means they are allocated on the heap and can be null. They are also called mutable type. It means that you can modify its value without creating a new object each time. StringBuilder performs faster than string when appending multiple string values.

C# String vs String Builder

Here is an example of a stringbuilder in C#.

StringBuilder sb = new StringBuilder("TutorialsTrend!!");

Difference Between String vs StringBuilder

S.No. String StringBuilder
1 It represents an immutable string. It represents a mutable string.
2 Immutable strings once created text cannot be changed Mutable strings once created text can be changed
3The string class is available in System Namespace.The StringBuilder class is available in System.Text Namespace
4Performance degrades when continuous change of value occurs Shows better performance since new changes are made to an existing instance
5string message = "TutorialsTrend!"; StringBuilder sb = new StringBuilder("TutorialsTrend!!");

Prev Next