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
What are immutable types in c#?
What is difference between field and property in c#?
What is difference between write and writeline in c#?
What is unrecognized escape sequence in c#?
Can an abstract class have a constructor c#?
write a program to find the biggest palindrome in the given string
what is IDisposal interface
What is the use of 'using' statement in c#?
What is c-sharp (c#)?
Can we overload the main method in c#?
What are constructors in c#?
How do you declare a method in c#?
What is ado c#?
What are bitwise logical operators?
Can class be protected in c#?