what is the difference between string and string builder with examples? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

what is the difference between string and string builder with examples?

please give me atleast one example because hr asked me

18th Sep 2016, 9:49 AM
venkata veerendra manikumar kancharla
venkata veerendra manikumar kancharla - avatar
2 Answers
+ 3
StringBuilder is preferable IF you are doing multiple loops, or forks in your code pass... however, for PURE performance, if you can get away with a SINGLE string declaration, then that is much more performant. For example: string myString = "Some stuff" + var1 + " more stuff" + var2 + " other stuff" .... etc... etc...; is more performant than StringBuilder sb = new StringBuilder(); sb.Append("Some Stuff"); sb.Append(var1); sb.Append(" more stuff"); sb.Append(var2); sb.Append("other stuff"); // etc.. etc.. etc.. In this case, StringBuild could be considered more maintainable, but is not more performant than the single string declaration. 9 times out of 10 though... use the string builder. On a side note: string + var is also more performant that the string.Format approach (generally) that uses a StringBuilder internally (when in doubt... check reflector!)
19th Sep 2016, 4:17 PM
Richard Blackburn
Richard Blackburn - avatar
0
thank you for giving answer
21st Sep 2016, 2:01 AM
venkata veerendra manikumar kancharla
venkata veerendra manikumar kancharla - avatar