Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6184213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:29:53+00:00 2026-05-24T01:29:53+00:00

So I am primarily a C# and Java developer but I suppose this question

  • 0

So I am primarily a C# and Java developer but I suppose this question could pertain to any programming languages that uses StringBuilder or some derivative thereof.

Okay so it is more or less common knowledge that concatenate even a small number of strings can be a major performance killer (although even that is debatable). My question is does anyone have knowledge of the performance effects of using a string builder within a string builder. To clarify what I mean by that let me include some dummy code that should help illustrate my point.

Also I realize that there is naturally a performance hit by calling multiple instances of StringBuilder but I do not believe that I would be calling it enough to cause any real performance issues. (This assumption may also be wrong any opinion on that would be helpful as well)

public string StringToGet()
{
     StringBuilder sb = new StringBuilder("Some Text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append(MethodThatCreatesAnotherString());
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append(MethodThatCreatesAnotherString());
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append(MethodThatCreatesAnotherString());
     //etc   

     return sb.toString();

}

private string MethodThatCreatesAnotherString()
{
     StringBuilder sb = new StringBuilder("Other text");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");

     return sb.ToString();
}

Logic tells me that this shouldn’t be a problem, but something about this approach just does not seem to look right to me. Can anyone shed some light on the following questions

  1. Does this create a significantly larger performance hit than just not using additional methods and using a single StringBuilder
  2. Is the a reasonably acceptable practice.

Any onsite on this would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:29:54+00:00Added an answer on May 24, 2026 at 1:29 am

    Typically, if the number of straight-up string concatenations are more than 5, it’s better to use StringBuilder. If you have control over the methods that in turn build a string, why not consider passing your StringBuilder into the method?

    public void MethodThatCreatesAnotherString(StringBuilder sb)
    {
       // code that appends to StringBuilder
    }
    

    So, when it’s time to call that function, you pass in your current StringBuilder, and you don’t lose a performance hit, but it’s at the expense of potential obfuscation.

    That said, unless you’re getting into the hundreds or thousands of these operations, I wouldn’t worry about premature optimization here. Your main performance hit is having to do it twice (i.e. append the strings inside the method, and then appending the complete string), but you still won’t be taking the performance hit to the extent of using + concatenation.

    Edit: A clarifying example of how to use it:

    string SomeMethodCreatingAString()
    {
       StringBuilder sb = new StringBuilder();
       sb.append("more text to add");
       sb.append("more text to add");
       sb.append("more text to add");
       sb.append("more text to add");
       sb.append("more text to add");
       MethodThatCreatesAnotherString(sb);
       // more text
       return sb.ToString();
    }
    

    So, you don’t ever append that string from the second method, you simply use the same StringBuilder within that method. Hopefully that makes sense.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My background is primarily as a Java Developer, but lately I have been doing
I wish that I had a more precisely descriptive title for this question, but
This is a question of curiosity about accepted coding practices. I'm (primarily) a Java
I have a question about Java style. I've been programming Java for years, but
I am primarily a .NET developer, and in that sphere alone there are at
Firstly, I realise that this is a very similar question to this one: Which
this is probably a n00b question, but even so... Apart from MS Office, I
1. Okay this is probably lame question, but I haven't found good result searching
I primarily work in Java, but I've recently started using ActionScript 3.0 for a
I'm a little shocked that (if?) this precise questions hasn't been asked, but 15

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.