GeekInterview.com
Series: Subject: Topic:
Question: 15 of 460

When to use string classes

when we have string builder which are mutable why we need immutable string classes in C#.
Asked by: sushil_gupta | Member Since Oct-2010 | Asked on: Jan 17th, 2011

View all questions by sushil_gupta   View all answers by sushil_gupta

Showing Answers 1 - 5 of 5 Answers
debdbada

Answered On : Jan 20th, 2011

View all answers by debdbada

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.

Yes  2 Users have rated as useful.
  
Login to rate this answer.
Vimlesh Kumar

Answered On : Sep 9th, 2011

View all answers by Vimlesh Kumar

when you dont want to do operations with string, you should use string class.

  
Login to rate this answer.

Stings are immutable, so when you are not performing any operations on strings, then use string class.

  
Login to rate this answer.
anupamsh

Answered On : Mar 7th, 2012

View all answers by anupamsh

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.

  
Login to rate this answer.
Sivavt

Answered On : Mar 25th, 2012

View all answers by Sivavt

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.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.