When to use string classes

When we have string builder which are mutable why we need immutable string classes in C#.

Questions by sushil_gupta   answers by sushil_gupta

Showing Answers 1 - 18 of 18 Answers

debdbada

  • Jan 20th, 2011
 

Strings are immutable, meaning unchanging, so continually building/adding to a string causes a whole new string to be written to memory.  Stringbuilder remedies that problem.

anupamsh

  • Mar 7th, 2012
 

This is how I would answer:

1) Strings are immutable and once created, cannot be changed. Hence, strings are best used in cases when you need thread safety and features where strings will not change, such as hash table keys.

1) StringBuilders should be used when you are continuously modifying the character array by deleting, appending characters from the buffer.

  Was this answer useful?  Yes

Sivavt

  • Mar 25th, 2012
 

String is immutable, meaning, if there is any change in the string value, run time allocates new memory & assign it to the string reference. For the programmer, it looks like the same string but internally, its a new string. It is better to avoid string and go for String Buffer in case modifications in string call is expected.

  Was this answer useful?  Yes

Denial

  • Aug 14th, 2013
 

String: String are immutable that is every time you make changes to a string object, the string is destroyed and a new string is created with modifications you applied to previous string. All this is done at run time.

String Builder: String builder are mutable which means you can do whatever modification you want without actually creating a new string.

It is advisable to use string builder only if there is extensive modification happen on string or string is of large length.

Denial

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions