C# Difference between String and StringBuilder with Examples

  By : Suresh Dasari
  Posted On : 02-May-2023

Here, we will learn what is string c#, how to use string c#, what is stringbuilder in c#, and the differences between string and StringBuilder in c# with examples.

What is String in C#?

In c#, the string is useful to hold the sequential collection of characters and it can be defined by using string keyword. The string is immutable which means the string object cannot be modified once it is created.

 

If you perform any operation like insert, update, delete, etc. on a string object, it will simply discard the old instance in the memory and create a new instance to hold the value.

C# String Example

In c#, we can define and initialize the values to string variables in different ways using string keyword.

 

// Defining the string variable
string str1;
// Defining and initializing the value
string str2 = "Welcome to Tutlane";
String str3 = "Hello World!";
// Initialize an empty string
string str4 = String.Empty;
// Initialize to null
String str5 = null;

In this string example, we defined the string variables using string and String keywords. Functionality wise both the keywords (string and String) are same. Here, the string keyword is an alias name for String keyword.

 

To learn more about strings, check out our C# String with Examples topic.

What is StringBuilder in C#?

In c#, the StringBuilder is same as the string to store the sequential collection of characters as text, but the only difference is the string is immutable, and the StringBuilder is mutable.

 

Mutability means once you create the StringBuilder instance in c#, the same instance will be used to perform any operation like insert, update, delete, etc. on the StringBuilder object. The StringBuilder class is available with System.Text namespace.

C# StringBuilder Example

Following is the example of defining and adding values to the StringBuilder in c#.

 

// Declaring the StringBuilder
StringBuilder sb1 = new StringBuilder();

// Declaring and assigning the value
StringBuilder sb2 = new StringBuilder(25);

// Declaring and appending multiple values
StringBuilder sb3 = new StringBuilder("Suresh");
sb3.Append(", Rohini");
sb3.Append(", Trishika");

To learn more about StringBuilder, check out StringBuilder in C# with Examples.

Differences between String and StringBuilder in C#

As discussed, the string and StringBuilder are useful to hold or store the collection of characters. The following table lists the differences between string and StringBuilder in c#.

 

StringStringBuilder
The string is useful for storing the sequential collection of characters. The StringBuilder is same as a string to store the collection of characters.
The string is available with System.String type. The StringBuilder is available with System.Text type.
The string is immutable and it cannot be modified once it is created. The StringBuilder is mutable and it can be modified after creation.
The string performance is almost same as StringBuilder. For a fixed number of operations, it is always advisable to use string. Compared to string, the StringBuilder performance is slightly better when you perform a large number of modify operations on string otherwise it's negligible.