How is String class different from StringBuilder class in .NET?

System namespace provides two main classes to deal with strings. The classes are String and StringBuilder respectively. Why do you really need two classes to deal with strings? How do they differ from each other? These questions are answered using the differences tabulated below:




String Class
StringBuilder Class
String class belongs to the namespace System. StringBuilder class belongs to the namespace System.Text.
String class is immutable. Immutable means that the string cannot be changed. Consider the following example:
class sampleClass {
public static void Main() {
string sampleStr = "Hai";
sampleStr = "Hello";
Console.WriteLine(sampleStr);
}
}
Output of this code will be:
Hello

In this example, you have created a string called sampleStr. You have initially assigned the value "Hai". And then you try to overwrite its value with "Hello". You get the overwritten value as output. But the problem lies in the number of strings that get created in memory. When you create the string as "Hai", this string gets created in the memory. When you try to change the value to "Hello", instead of overwriting the existing "Hai" string it will create a new string in the memory and assign this new string "Hello" to sampleStr.

StringBuilder class is mutable. Consider the following example:
class sampleClass {
public static void Main() {
StringBuilder sampleSB = new
StringBuilder("Hai",10);
sampleSB = new
StringBuilder("Hello");
Console.WriteLine(sampleSB);
}
}
Output of this code will be:
Hello

In this example, you are doing the same thing. But the string "Hai" will be overwritten as "Hello" and no new strings will be created in the memory.

You can directly assign a string to string class instance. For example,
String sampleStr = "Hai" is valid.
You cannot directly assign a string to StringBuilder instance. For example,
StringBuilder sampleSB = "Hai" will lead to the following error:
"cannot implicitly convert type 'string' to 'System.Text.StringBuilder' "
You can assign a string to StringBuilder using the following statement:
StringBuilder sampleSB = new
StringBuilder("Hai");
String concatenation is done using + operator. Here is an example:
class sampleClass {
public static void Main() {
string sampleStr = "Hello!";
sampleStr += " Good Day!";
Console.WriteLine(sampleStr);
}
}
Output of this code will be:
Hello! Good Day!

Here you have used += operator to perform both concatenation and assignment using

String concatenation is done using Append method. Here is an example:
class sampleClass {
public static void Main() {
StringBuilder sampleSB = new
StringBuilder("Hello!");
sampleSB.Append("Good
Day!");
Console.WriteLine(sampleSB);
}
}
Output of this code will be:
Hello! Good Day!
String concatenation is done using + operator. Here is an example:
class sampleClass {
public static void Main() {
string sampleStr = "Hello!";
sampleStr += " Good Day!";
Console.WriteLine(sampleStr);
}
}
Output of this code will be:
Hello! Good Day!

Here you have used += operator to perform both concatenation and assignment using single operator. You can also use + and = separately as shown below:
sampleStr = sampleStr + " Good Day!";

 
During string concatenation, additional memory will be allocated. During string concatenation, additional memory will be allocated if and only if the string buffer's capacity is reached.
During string concatenation, additional memory will be allocated if and only if the string buffer's capacity is reached. If the number of concatenations to be done is random or not known, then it is recommended to use stringBuilder
You cannot set a limit (specifying how many strings can be concatenated) to a string object using string class. You can set a limit to StringBuilder using the member called capacity which will by default have the value 16. You can override it to any number. The maximum value acceptable is equivalent to MaxValue of Int32. If you feel that you do not want to reserve 16 as the capacity then you can very well redefine it. However the capacity will dynamically grow based on the number of strings that you append.

Here is an example demonstrating the usage of capacity:
class sampleClass {
public static void Main() {
StringBuilder sampleSB = new
StringBuilder();
Console.WriteLine(
sampleSB.Capacity);
sampleSB.Capacity = 1;
Console.WriteLine(
sampleSB.Capacity);
sampleSB.Append("str1");
sampleSB.Append("str2");
Console.WriteLine(
sampleSB.Capacity);
}
}
Output of this code will be:
16
1

8

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| How do you implement Observer Design Pattern in .NET? | How do you pass data between different Tiers in .NET Architecture? | How is Classic ADO different from ADO.NET? | How is Dataadapter useful in ADO.NET? | How is Datareader different from Dataset in ADO.NET? | How is .NET Application Development different from Traditional Development? | How is HashTable different from ArrayList in .NET? | How is Inheritance achieved in C#? | How is new keyword different from override keyword during inheritance in .NET? | How is String class different from StringBuilder class in .NET? | Illustrate ADO.NET Architecture | Illustrate the importance of Server.Transfer and Response.Redirect in .NET? | Mention the different objects available in Dataset of ADO.NET | Mention the usage of Connection Object in ADO.NET | What are the commonly used methods of Dataadapter in ADO.NET? | What are the different Behavioral Design Patterns that can be used in .NET Architecture? | What are the different Creational Design Patterns that can be used in .NET Architecture? | What are the different Structural Design Patterns that can be used in .NET Architecture? | What are the methods provided by Command Objects in ADO.NET? | What is Internal Access Modifier in C#? |


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.