string and string builder class [Resolved]

Posted by Bhupentiwari under ASP.NET on 6/26/2012 | Points: 10 | Views : 1584 | Status : [Member] | Replies : 3
what is the difference between string and string builder class

Thanks n Regards
Bhupendra Tiwari



Responses

Posted by: Bandiranjithreddy on: 6/27/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
Well, the most important difference between String and StringBuffer/StringBuilder in C# is that String object is immutable whereas StringBuffer/StringBuilder objects are mutable.

By immutable, we mean that the value stored in the String object cannot be changed. Then the next question that comes to our mind is "If String is immutable then how am I able to change the contents of the object whenever I wish to?" . Well, to be precise it's not the same String object that reflects the changes you do. Internally a new String object is created to do the changes.

So suppose you declare a String object:

String myString = "Hello";

Next, you want to append "Guest" to the same String. What do you do?

myString = myString + " Guest";

When you print the contents of myString the output will be "Hello Guest". Although we made use of the same object(myString), internally a new object was created in the process. So, if you were to do some string operation involving an append or trim or some other method call to modify your string object, you would really be creating those many new objects of class String.

Now isn't that a performance issue?

Yes, it definitely is.

Then how do you make your string operations efficient?

By using StringBuffer or StringBuilder.

How would that help?

Well, since StringBuffer/StringBuilder objects are mutable, we can make changes to the value stored in the object. What this effectively means is that string operations such as append would be more efficient if performed using StringBuffer/StringBuilder objects than String objects.

Finally, whats the difference between StringBuffer and StringBuilder?

StringBuffer and StringBuilder have the same methods with one difference and that's of synchronization. StringBuffer is synchronized( which means it is thread safe and hence you can use it when you implement threads for your methods) whereas StringBuilder is not synchronized( which implies it isn't thread safe).

So, if you aren't going to use threading then use the StringBuilder class as it'll be more efficient than StringBuffer due to the absence of synchronization.

Enjoy the coding :)

Bhupentiwari, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhupentiwari on: 6/27/2012 [Member] Starter | Points: 25

Up
0
Down
thanx budy

Thanks n Regards
Bhupendra Tiwari

Bhupentiwari, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gemamgall on: 6/27/2012 [Member] Starter | Points: 25

Up
0
Down
StringBuilder is more efficient in cases where there is a large amount of string manipulation As System.String are immutable, so each time a string is changed, a new instance in memory is created.

Thanks and Regards,

Vinay Gandhi
www.quadtechindia.com

Bhupentiwari, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response