20
Reply

When to use StringBuilders?

Arunava Bhattacharjee

Arunava Bhattacharjee

Aug 26, 2014
4.3k
0

    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.

    Manish Kumar Choudhary
    November 18, 2014
    2

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

    Mohsin Mukri
    July 06, 2017
    0

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

    Mohsin Mukri
    July 06, 2017
    0

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

    Ashish Srivastava
    February 04, 2016
    0

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

    sushil kumar
    January 04, 2016
    0

    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.

    Ajay Gandhi
    November 29, 2015
    0

    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.

    Ajay Gandhi
    November 29, 2015
    0

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

    Srikanth Reddy
    June 17, 2015
    0

    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

    Abrar Ahmad Ansari
    March 10, 2015
    0

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

    Md. Raskinur Rashid
    January 20, 2015
    0

    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.

    Yadagiri Reddy
    October 17, 2014
    0

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

    trilochan
    October 14, 2014
    0

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

    Roymon TP
    October 09, 2014
    0

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

    Roymon TP
    October 09, 2014
    0

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

    Roymon TP
    October 09, 2014
    0

    http://www.dotnetperls.com/stringbuilder

    Munesh Sharma
    October 09, 2014
    0

    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

    Mahesh Kumar alanka
    September 18, 2014
    0

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

    Mohit Kumar
    September 06, 2014
    0

    StringBuilders is mutable class. this string can be changed

    deepak verma
    September 04, 2014
    0

    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.

    Arunava Bhattacharjee
    August 26, 2014
    0