What are the difference between string and string Builder?


The following are the difference between string and StringBuilder

1. String is Immutable which will create new string object from System. String class in memory.

While String Builder is mutable (Fixed) so it will not allocated in memory.

2.If we have to do much string insertion manipulation then we have to use string builder. It is mutable. It will save the memory. But string is immutable so it will create a new memory every time.

For example

StringBuilder returnNumber = new StringBuilder(10000);

for(int i = 0; i<1000; i++)

{

returnNumber.Append(i.ToString());

}

But if we have to do very small work like simple concatenation then we have to use string.

String a=”test1”;

String b =”test2”;

String c=a+b;

Summary:

String Type:
1.It has immutable buffers.
2.It Cannot be changed.
3.Each operation returns a new string.
4.Copies can increase memory pressure.

StringBuilder type:
1.It has mutable buffers.
2.It Can be changed without copying.
3.It will be helpful in long loop condition.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: