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.
Here is an example of a string in C#:
string message = "TutorialsTrend!";
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.
Here is an example of a stringbuilder in C#.
StringBuilder sb = new StringBuilder("TutorialsTrend!!");
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 |
3 | The string class is available in System Namespace. | The StringBuilder class is available in System.Text Namespace |
4 | Performance degrades when continuous change of value occurs | Shows better performance since new changes are made to an existing instance |
5 | string message = "TutorialsTrend!"; | StringBuilder sb = new StringBuilder("TutorialsTrend!!"); |