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

  • SEARCH
  • Home
  • 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 7855459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:23:32+00:00 2026-06-02T20:23:32+00:00

Which is more efficient in C#, 1 or 2? StringBuilder sb = new StringBuilder();

  • 0

Which is more efficient in C#, 1 or 2?

StringBuilder sb = new StringBuilder();
sb.Append("my string " + myVar + " my string");     // 1
sb.AppendFormat("my string {0} my string", myVar);  // 2

I’m guessing that the question could also be rephrased:

string y = "my string " + myVar + " my string";              // 1
string x = String.Format("my string {0} my string", myVar);  // 2
  • 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-06-02T20:23:33+00:00Added an answer on June 2, 2026 at 8:23 pm

    Version of .NET Framework is important here, because implementation of StringBuilder.Append and StringBuilder.AppendFormat can differ significantly between individual versions. Under .NET Framework 4, (1) is faster than (2), but it is still inefficient because of overhead caused by concatenating (and thus copying) of (sub)strings. This is even 2x faster than (1):

    StringBuilder sb = new StringBuilder();
    sb.Append("my string");
    sb.Append(myVar);
    sb.Append(" my string");
    

    UPDATE:
    Using following test:

    static void Main(string[] args)
    {
        string myVar = "abcdef";
        Stopwatch stopwatch = Stopwatch.StartNew();
    
        for (int j = 0; j < 10000; j++)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 10000; i++)
            {
                //UNCOMMENT ONE OF THESE TESTS
                //Test1
                sb.Append("my string " + myVar + " my string");
    
                //Test2
                //sb.AppendFormat("my string {0} my string", myVar);
    
                //Test3
                //sb.Append("my string ");
                //sb.Append(myVar);
                //sb.Append(" my string");
            }
        }
        stopwatch.Stop();
        Console.WriteLine(stopwatch.ElapsedMilliseconds + " ms");
    }
    

    I meassured these results on my computer (Intel Core2 Q9400, Windows Server 2008 x64, .NET Framework 4.0, Release mode):

    • Test1: 10401 ms
    • Test2: 20262 ms
    • Test3: 5771 ms
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which is more efficient, "data.Length==0" or "data==string.Empty"? in this question there was an answer
I know that StringBuilder is more efficient than a normal string when processing code
I am wondering which is more efficient, to store temporary data (related to that
My real Question is which of these is more efficient in terms of loading
Also in the following code, which is more efficient? if (Length(ParamStr(1)) <> 0) then
Quick question, which is more efficient for finding the smallest number (float) in a
I'm new to Java. Which is more efficient for Vectors -- clear() or removeAllElements().
The question is simply between $(body).click(function(e){}); vs $(document).click(function(e){}); which is more efficient or advisable?
I'm wondering which is more efficient. std::map< String, std::set<int> > or std::multimap< String, int
Two Parts two my question. Which is more efficient/faster: int a,b,c,d,e,f; int a1,b1,c1,d1,e1,f1; int

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.