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

Answers were Sorted based on User's Feedback



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

Answer / k.saisagar

In Dot Net there will be many cases where in we need to operate on strings and first thing we remember is using system.String but there are certainly many points we need to remember and consider before we operate on strings.

1) Performance.

2)how many times we need to concatenate.

Lets take an example for concatenate five strings.

EX 1. Using System.String

System.String str =“My Name is dilip”;

str += “and i am”; str += “working on “;

str += “Post of “;

str += “difference between string and string builder”;

Response.Write(str);

and the expected output well you probably guessed it right

“My Name is dilipand i amworking on Post of difference between string and string builder”

Now what has happed? yes the important question now how many times we have appended the str variable those number of times string was created in memory location and abandoned when a new string is created and later waiting for garbage collection.

This leads to memory wastage and degradation of performance because string are immutable(that means any change to string causes a runtime to create a new string and abandon old one).

Think about the situation where in u need to work on 100 or more strings????

Dot Net has answer for it in the form of System.Text.StringBuilder class

EX2. Same Example using StringBuilder

StringBuilder sb = new StringBuilder();

sb.Append( “My Name is dilip”);

sb.Append( “and i am”);

sb.Append( “working on “);

sb.Append( “Post of “);

sb.Append( “difference between string and string builder”);

and the output is same as previous

“My Name is dilipand i amworking on Post of difference between string and string builder”

But this time there was only one string created in memory dynamically and modified as we append the new string, by this there is not much garbage collection and also helps improve performance. Append is taken only for example there are a lot of other functions which are just waiting for you to invoke.Happy coding.

Is This Answer Correct ?    1 Yes 0 No

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

Answer / praveen kumar a

Strings are so heavily used in all programming languages
that we do not think about them very much.Normally all goes
well but sometimes we need more performance so we switch to
StringBuilder which is more efficient because it does
contain a mutable string buffer. .NET Strings are immutable
which is the reason why a new string object is created
every time we alter it (insert, append, remove, etc.).

Is This Answer Correct ?    0 Yes 0 No

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

Answer / wavare santosh

What is the difference between String and StringBuilder?

Both String and StringBuilder are classes used to handle
strings.

The most common operation with a string is concatenation.
This activity has to be performed very efficiently. When we
use the "String" object to concatenate two strings, the
first string is combined to the other string by creating a
new copy in the memory as a string object, and then the old
string is deleted. This process is a little long. Hence we
say "Strings are immutable".

When we make use of the "StringBuilder" object, the Append
method is used. This means, an insertion is done on the
existing string. Operation on StringBuilder object is
faster than String operations, as the copy is done to the
same location. Usage of StringBuilder is more efficient in
case large amounts of string manipulations have to be
performed.

Is This Answer Correct ?    0 Yes 0 No

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

Answer / 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

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

Answer / amit chaudhary

String and stringbuilder both methods are used to handle the
strings . the main differece between them in string
everytime a object has to be created for Operations like
append,Insert etc. at runtime but in case of stringbuilder
we can operate operations on same object. means the string
method is mutable and stringbuilder method is immutable

Is This Answer Correct ?    15 Yes 20 No

Post New Answer

More C Sharp Interview Questions

If you want to write your own dot net language, what steps you will u take care?

0 Answers  


Explain when should you call the garbage collector in .net?

0 Answers  


What is delegates in c# and uses of delegates?

0 Answers  


What is unrecognized escape sequence in c#?

0 Answers  


What are the different types of delegates?

0 Answers  






What are the access-specifiers available in c#?

26 Answers   Syntel,


Is arraylist thread safe?

0 Answers  


Explain About the Sattilite Assembly in .Net Technology?

2 Answers   TCS,


What are Indexers in C#?

0 Answers   SwanSoft Technologies,


Is comparator a functional interface?

0 Answers  


Where do we use static class in c#?

0 Answers  


the selected inf of other combobox?How do you achieve it?

1 Answers   CTS, Microsoft,


Categories