Udemy logo

stringbuilder c#In order to store a string type data, the .NET framework contains a String class that enables programmers to manipulate text strings. The String class comes with a variety of built-in utility functions that can be used to achieve a myriad of functionalities over strings. Although the String class is sufficient enough to perform almost every function, it has a potential deficiency. The objects of the String class are immutable, which means that once the object of the String class has been created, it cannot be changed or modified.

For instance, if a String type object contains a value “abcd”, then this object is assigned a new value of “wxyz”. The memory address that contains string “abcd” is not modified; rather, a new object is created with a value containing “wxyz” and the reference of the variable that was previously pointing to the object containing “wxyz” is modified to refer to new object that contains “abcd”. Similarly, String type functions that modify String objects do not actually modify the objects. They create new objects and store the modified value into those new objects.

Are you new to C#? Udemy.com has a beginner’s course for you here

The .NET framework contains a class that can be used for storing mutable strings or the strings that can be changed. This functionality is achieved using the StringBuilder class.

What is the StringBuilder Class in C#?

StringBuilder is a C# class that can be used to store and manipulate string  data types, which when modified returns the same StringBuilder object with modified values rather than returning a new object. The following example demonstrates the difference between a String class and StringBuilder class.

StringBuilder C#: A Basic Example

class Program
static void Main(string[] args)
String content = "abcd";
content = content + "wxyz";
StringBuilder content2 = new StringBuilder("abcd");
content2 = content2.Append("wxyz");

In the above example, first a String type variable is instantiated by storing a string literal value of “abcd”. At this time, the content variable points to the String type object in the memory space that contains “abcd”. In the next line of code, another string literal “wxyz” is appended to the string. What happens is that instead of modifying the object that contains “abcd”, a new object is instantiated in the memory and content variable is made to refer to this new object.

Contrarily, in the above example, a StringBuilder object named content2 is instantiated and assigned a string value of “abcd”. Next, the Append method is called on the content2 variable and a string value of “wxyz” is passed. This appends “wxyz” at the end of StringBuilder object content2. But in this case, a new object is not created in memory; rather, the object that contained the value “abcd” is made to accommodate the appended string “wxyz”.

StringBuilder might not look of much benefit at the moment. However, when string modification is done at a huge rate, creating a new string and then copying the modified content of the old String to a new String object can be extremely resource-intensive tasks. In such scenarios, it is always advisable to use StringBuilder objects rather than simple String type objects.

To learn more about StringBuilder in C#, take this course at Udemy.com

Some important StringBuilder functions

In the last example, the Append method was demonstrated. There are few other important StringBuilder functions that can be used to gain maximum benefit while using string data types in software applications.

The append line method is used to insert a new line in the String. The following example demonstrates the usage of the AppendLine method.

StringBuilder content = new StringBuilder("Udemy has the best tutorials");
content.Append("From all walks of life.");

In the above example, a StringBuilder object named content is instantiated with a string value. Next, the AppendLine method is called on this object. This appends a line break to the content string. Then, another string value is appended to the content string, which is then added in the next line.

As the name suggests, the Clear method is used to clear all the content from the object. The following example demonstrates the working of Clear:

StringBuilder content = new StringBuilder("Udemy.com");

In the above example, the StringBuilder object content contains a string value that is displayed on the console. Next, the Clear method is called on the content variable. The value of the content variable is displayed on the console and this time nothing is displayed since the content variable contains nothing but an empty string.

The Replace method is used to replace characters or a substring in a string with a new string. The Replace method in StringBuilder is similar to Replace method of the String class but in this case, the Replace method modifies the original object rather than creating and returning a new modified object. The following example demonstrates the Replace method:

StringBuilder content = new StringBuilder("Udemy.com is best");
content.Replace("best", "great");

This method is used to insert a variety of data types in a StringBuilder object depending on the type of overload used. The Insert method takes two parameters: Zero based index where the value has to be inserted and the value itself. The following example demonstrates the usage of the Insert method:

StringBuilder content = new StringBuilder("Football");
content.Insert(4, "great");

In the above example, the content object contains the string value “Football”. The Insert method is then used to insert the string “great” at index 4. The resulting string is Footgreatball.


The StringBuilder class is a mutable variation of the more conventional String class that stores the string data type. The StringBuilder class stores string values that can be modified without having to instantiate a new object. The StringBuilder class should typically be used in scenarios where there is a large number of string values that need modification.

Interested in learning more about StringBuilder in C#, check out this course at Udemy.com

Page Last Updated: June 2014

Top courses in C#

Complete C# Unity Game Developer 3D
Ben Tristem, Rick Davidson, GameDev.tv Team, Gary Pettie
4.7 (37,774)
C#/.NET - 50 Essential Interview Questions (Junior Level)
Krystyna Ślusarczyk
4.9 (301)
Highest Rated
Design Patterns in C# Made Simple
Zoran Horvat
4.4 (304)
C#/.NET - 50 Essential Interview Questions (Mid Level)
Krystyna Ślusarczyk
4.8 (131)
C# Intermediate: Classes, Interfaces and OOP
Mosh Hamedani
4.5 (31,557)
Learn Parallel Programming with C# and .NET
Dmitri Nesteruk
4.5 (3,176)
Advanced Topics in C#
Dmitri Nesteruk
4.6 (354)
RPG Core Combat Creator: Learn Intermediate Unity C# Coding
Ben Tristem, Rick Davidson, GameDev.tv Team
4.7 (10,397)
Design Patterns in C# and .NET
Dmitri Nesteruk
4.5 (10,272)
Complete C# Masterclass
Denis Panjuta, Tutorials.eu by Denis Panjuta
4.5 (23,879)

More C# Courses

C# students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo