Arunava Bhattacharjee
When to use StringBuilders?
By Arunava Bhattacharjee in C# on Aug 26 2014
  • Manish Kumar Choudhary
    Nov, 2014 18

    Generally inside loop block where string value is change frequently we use stringBuilder class object. Actually String object is immutable. Every time we use one of the methods in the System.String class, we create a new string object in memory, which requires a new allocation of space for that new object. so when frequent change in string value is happening it's recommended to use StringBuilders.

    • 4
  • Sreekanth Reddy
    Jun, 2015 17

    If concatination is going to be done for multiple times to a string variable, then we can go for StringBuilder.

    • 2
  • Mohsin Mukri
    Jul, 2017 6

    when we are concatenating string value for the multiple iterations because the string builder handle large number of character with the greater performance

    • 0
  • Mohsin Mukri
    Jul, 2017 6

    when we are concatenating string value for the multiple iterations because the string builder handle number of character with the greater performance

    • 0
  • Ashish Srivastava
    Feb, 2016 4

    When we required string manipulation. StringBuilder is faster than String. using System.Text; namespace in C#

    • 0
  • sushil kumar
    Jan, 2016 4

    when you want to add or concanate, substract etc. on string manipulation that time you can use stringbuilder. StringBuilder is faster than string.

    • 0
  • Ajay Gandhi
    Nov, 2015 29

    Definitely use StringBuilder when you're concatenating in a non-trivial loop - especially if you don't know for sure (at compile time) how many iterations you'll make through the loop. For example, reading a file a character at a time, building up a string as you go using the += operator is potentially performance suicide. Definitely use the concatenation operator when you can (readably) specify everything which needs to be concatenated in one statement. (If you have an array of things to concatenate, consider calling String.Concat explicitly - or String.Join if you need a delimiter.) Don't be afraid to break literals up into several concatenated bits - the result will be the same. You can aid readability by breaking a long literal into several lines, for instance, with no harm to performance. If you need the intermediate results of the concatenation for something other than feeding the next iteration of concatenation, StringBuilder isn't going to help you. For instance, if you build up a full name from a first name and a last name, and then add a third piece of information (the nickname, maybe) to the end, you'll only benefit from using StringBuilder if you don't need the (first name + last name) string for other purpose (as we do in the example which creates a Person object). If you just have a few concatenations to do, and you really want to do them in separate statements, it doesn't really matter which way you go. Which way is more efficient will depend on the number of concatenations the sizes of string involved, and what order they're concatenated in. If you really believe that piece of code to be a performance bottleneck, profile or benchmark it both ways.

    • 0
  • Ajay Gandhi
    Nov, 2015 29

    Definitely use StringBuilder when you're concatenating in a non-trivial loop - especially if you don't know for sure (at compile time) how many iterations you'll make through the loop. For example, reading a file a character at a time, building up a string as you go using the += operator is potentially performance suicide. Definitely use the concatenation operator when you can (readably) specify everything which needs to be concatenated in one statement. (If you have an array of things to concatenate, consider calling String.Concat explicitly - or String.Join if you need a delimiter.) Don't be afraid to break literals up into several concatenated bits - the result will be the same. You can aid readability by breaking a long literal into several lines, for instance, with no harm to performance. If you need the intermediate results of the concatenation for something other than feeding the next iteration of concatenation, StringBuilder isn't going to help you. For instance, if you build up a full name from a first name and a last name, and then add a third piece of information (the nickname, maybe) to the end, you'll only benefit from using StringBuilder if you don't need the (first name + last name) string for other purpose (as we do in the example which creates a Person object). If you just have a few concatenations to do, and you really want to do them in separate statements, it doesn't really matter which way you go. Which way is more efficient will depend on the number of concatenations the sizes of string involved, and what order they're concatenated in. If you really believe that piece of code to be a performance bottleneck, profile or benchmark it both ways.

    • 0
  • Abrar Ahmad Ansari
    Mar, 2015 10

    if you want to append large string data then use string builder. for more details refer below linkhttp://www.aspdotnet-suresh.com/2013/11/difference-between-string-and-stringbuilder-csharp.html

    • 0
  • Md. Raskinur Rashid
    Jan, 2015 20

    Simple. when you try to build a string...

    • 0
  • Yadagiri Reddy
    Oct, 2014 17

    string builder gives high performance. it mainly used whenever you add the values in runtime(if you don't know how many values are there). it is mutable and it uses the Append or AppendLine methods for Adding new item.

    • 0
  • Trilochan Tripathi
    Oct, 2014 14

    FOR FASTER PERFORMANCE WHILE USING STRING CONCATENATION ETC USE STRING BUILDERS. STRING BUILDERS ARE MUTABLE.

    • 0
  • Roymon TP
    Oct, 2014 9

    When we concatenate string ,or use in strings operations inside loop, use string builder.

    • 0
  • Roymon TP
    Oct, 2014 9

    When we concatenate string ,or use in strings operations inside loop, use string builder.

    • 0
  • Roymon TP
    Oct, 2014 9

    When we concatenate string ,or use in strings operations inside loop, use string builder.

    • 0
  • Munesh Sharma
    Oct, 2014 9

    http://www.dotnetperls.com/stringbuilder

    • 0
  • Mahesh Kumar Alanka
    Sep, 2014 18

    If we want to do some operations on strings like concat,trim,reverse,... we can use StringBuilder class.we are unable to do operations on String class

    • 0
  • Mohit Kumar
    Sep, 2014 6

    To make string mutable we need to use it is a class basically for similar kind c# interview questions refer this link

    • 0
  • deepak verma
    Sep, 2014 4

    StringBuilders is mutable class. this string can be changed

    • 0
  • Arunava Bhattacharjee
    Aug, 2014 26

    Tricky question.Some people has the knowledge when we get multiple string concat operations we chose StringBuilder over Strings. But we need to remember that StringBuilder internally uses array.What if the concat operation happens only twice?Which one is too chose.My experience tells me, I would like to chose StringBuilder over Strings when there is consequitive concat operations more than 6 times. Otherwise, I will chose simple Strings.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS