Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

What is the Main difference between String and
StringBuilder and why do we use StringBuilder.

Answer Posted / umarali1981

String:

1.String class belongs to the namespace System.
2. 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.
3.You can directly assign a string to string class instance.
For example,
String sampleStr = "Hai" is valid.
4. 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!";
5.During string concatenation, additional memory will be
allocated.
6.During string concatenation, additional memory will be
allocated if and only if the string buffer's capacity is
reached.
7.You cannot set a limit (specifying how many strings can be
concatenated) to a string object using string class.

StringBuilder:
1.StringBuilder class belongs to the namespace System.Text.
2. 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.
3.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");
4.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!
5.During string concatenation, additional memory will be
allocated if and only if the string buffer's capacity is
reached.
6.If the number of concatenations to be done is random or
not known, then it is recommended to use stringBuilder
7. 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

REFERENCE:
http://onlydifferencefaqs.blogspot.in/2012/08/oops-difference-faqs-4.html

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the console on a mac?

868


What are boxing and unboxing?

868


How can it prevents DLL Hell assembly versioning in .NET?

966


What is autopostback in c#?

890


To allow an element to be accessed using a unique key which .NET collection class is used ?

1039


What is interface used in c#?

961


What is the difference between out and ref in c#?

947


how to compare numbers and dispaly the largest ? *first thing I wanted to do is to input how many numbers to be compared *and then analyzed the largest then display it.

1665


What are the generation of Garbage Collection in C# .NET ?

912


Define property in c#.net?

905


What is icomparable in c#?

909


Write a program in c# to find the angle between the hours and minutes in a clock?

861


What is the difference between as and is operators in c#?

869


What is the use of parse in c#?

833


What is tryparse c#?

888