C# Interview Questions and Answers
<< Previous Question Next Question >>
 Question: 1550 Page Views: 

What is the difference between String and StringBuilder type?



Posted By: Avi Date: 15 October 2009 04:12:05 AM
 Answer:

String type is immutable while a StringBuilder type is mutable. StringBuilder objects performs better as compared to String objects.

An immutable object is an object whose state cannot be modified after it is created.

When we declare a String variable using following statement, it creates a String object in memory:

String strTest = "StringData";

Now if we modify this String variable' value using following statement:

strTest = "ModifiedStringData";

This discards old variable and creates a new variable with new value in memory.

But StringBuilder objects uses the same space in memory even if you change the value. So StringBuilder objects performs better as compared to String objects.


Posted By: eTechPlanet


Date: 15 October 2009 04:12:05 AM
Post a better Answer if you have
 
(Will show your Gravatar icon)  
  Country flag

Loading
Enter the text as shown in the image pdZAKI
Related Questions
C# : What is background worker and how do you use it?

What is background worker and how do you use it?

BackgroundWorker is class that allows to run long operations in background thread. The DoWork event ....
Category: C# Date: 10/14/2010 6:48:14 AM
C# : How do you use delegates(write code using Action delegate)?

How do you use delegates(write code using Action delegate)?

Action a = () => Console.WriteLine(“something”); a.Invoke();
Category: C# Date: 10/14/2010 6:45:31 AM
C# : Can as operator perform user defined conversion?

Can as operator perform user defined conversion?

No
Category: C# Date: 10/14/2010 6:43:14 AM
C# : What is var keyword?

What is var keyword?

Var means that variable is implicitly typed, it’s strongly typed but the compiler determines the typ....
Category: C# Date: 10/14/2010 5:22:31 AM
C# : How to initialize Dictionary<int, string> using collection initialize?

How to initialize Dictionary using collection initialize?

Dictionary<int, string> t = new Dictionary<int, string>() { {1, “a”}, {2, “vb”}, {3, “c”....
Category: C# Date: 10/14/2010 5:01:31 AM