Skip to main content

Difference between String and StringBuilder class in Asp.Net C# ?


String
and StringBuilder class are used to handle the strings but there are subtle differences between these two.
Let's understand the difference between String and StringBuilder using suitable example:
1. Performance of StringBuilder is high as compared to String
Consider the following concatenation example:
String str= "";
str += "Welcome ";
str += "to ";
str += "kunalsaurav.com";
Output string will be Welcome to kunalsaurav.com
Let's do the same concatenation operation using StringBuilder class
StringBuilder sb = new StringBuilder("");
sb.Append("Welcome");
sb.Append("to ");
sb.Append("kunalsaurav332.com");
String str = sb.ToString();
Output string will be Welcome to kunalsaurav332.com
Have you noticed the result using String and StringBuilder is same; then what is the difference between these two? The difference is in their performance. Whenever we concatenate new string to the old string then String class always create new object of string type in memory where as when you use string builder class for that purpose, it updates string at one place in memory and does not create new space in memory.
So obviously StringBuilder consumes less memory thus perform faster operation. So it is always recommended to use StringBuilder class for operations that manipulate strings. StringBuilder will give better performance as compared to String when concatenating the strings like shown above in example.
2) From the above point it is clear that String consumes more memory as compared to StringBuilder
3) String Concatenation can be done using '+' operator or using the String.Concat method whereas in case of StringBuilder we use Append() method to concatenate.
4) String is Immutable (Non Modifiable) whereas StringBuilder is a Mutable Class
String is immutable i.e. the value stored in the String object cannot be changed. We cannot change it after creating it. Every time we use one of the methods in the System.String class that manipulate the string, we are actually creating a new string object in memory, which requires a new allocation of space for that new object. Internally a new String object is created each time whenever we do the changes.
For example, when we use the "String" object to concatenate two strings, the first string is concatenated to the other string by creating a new copy in the memory as a string object, and then the old string is deleted so we can say when we create a string we can never change it, rather it will create a new string to store the new value.
Hence it can cause performance issue in situations where we need to perform repeated modifications to the string because overhead associated with creating a new String object can be costly.
StringBuilder is mutable. By mutable we mean that we can perform the operations that manipulate the string without creating the new StringBuilder object each time.
For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop. So StringBuilder is more efficient if large amounts of string manipulations have to be performed.
4) String class belongs to the namespace System while StringBuilder class belongs to the namespace System.Text.
5) When string concatenation takes place, additional memory will be allocated whereas in case of StringBuffer additional memory will be allocated only when the string buffer capacity exceeds.
When to use String or StringBuilder Class?
For simple String manipulations we can use String class. But for large string manipulations it is recommended to use StringBuilder class.

Comments

Popular posts from this blog

What is cookie? Advantages and disadvantages of cookies?

What is cookie? A cookie is a small piece of text file stored on user's computer in the form of name-value pair. Cookies are used by websites to keep track of visitors e.g. to keep user information like username etc. If any web application using cookies, Server send cookies and client browser will store it. The browser then returns the cookie to the server at the next time the page is requested. The most common example of using a cookie is to store User information, User preferences, Password Remember Option etc.It is also one of the common and mostly asked interview questions. Some facts about Cookie Here are a few facts to know about cookies: · Cookies are domain specific i.e. a domain cannot read or write to a cookie created by another domain. This is done by the browser for security purpose. · Cookies are browser specific. Each browser stores the cookies in a different location. The cookies are browser specific and so a cookie created in one browser(e.g in Google Chrome

Code First Getting Started

In this tutorial let us create a simple application to demonstrate the use of entity framework using code first. We are using Visual Studio 2015 and entity framework 6.1.3. You can download Visual Studio community Edition . You should have the basic knowledge of .Net framework, C# and MS SQL Server. In this tutorial, we will create a simple application with a user class.  Our user class will have basic information like name and email address of the user. Create the Project Open Visual Studio. File ->New -> Project Select C# -> Select Console Application Name the application as “EFGettingStarted” Click on OK Install Entity Framework The next step is to install the Entity framework. This can be installed via nuget package console. Click on Tools->Nuget Package manager -> Package Manager Console and type the following command C# 1 2 3   install - package entityframework   This will install the late

First, FirstOrDefault, Single, SingleOrDefault In C#

For people who are new to LINQ, it is difficult to understand the difference between First, FirstOrDefault, Single, SingleOrDefault. In this blog, I will explain what to use and when.     I will take a simple example to make you understand practically how these methods work.   Consider a class Employee with properties as Id, Name, and Department. class  Employee {    public   int  Id {  get ;  set ; }    public   string  Name {  get ;  set ; }    public   string  Department{  get ;  set ; } } I have a list of Employees: List<Employee> employeeList =  new  List<Employee>(){    new  Employee() { Id = 1, Name =  "Sunny" , Department =  "Technical"  },    new  Employee() { Id=2, Name= "Pinki" , Department = "HR" },    new  Employee() { Id=3, Name= "Tensy" , Department = "Finance" },    new  Employee() { Id=4, Name= "Bobby" , Department = "Technical" },    new