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 424603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:17:47+00:00 2026-05-12T19:17:47+00:00

I have a situation where I need to concatenate several string to form an

  • 0

I have a situation where I need to concatenate several string to form an id of a class. Basically I’m just looping in a list to get the ToString values of the objects and then concatenating them.

foreach (MyObject o in myList)
  result += o.ToString();

The list is NOT expected to have more than 5 elements (although it could but that’s a very, very marginal case) and usually will have from 1 to 3 elements, being common for it to just have one or two.

What would be more performance, keeping the concatenation or using an StringBuilder?

StringBuilder bld = new StringBuilder()
foreach (MyObject o in myList)
  bld.Append(o.ToString());

I’m unsure if creating the StringBuilder will take more time than standard concatenation for the most usual case.

This is lazy, items on the list do not change once created so the id is lazily constructed once when called.

As a side note… Should I use a fixed array instead of a List? Would I get any performance or memory improvement if I do? (List is only used as an IEnumerable anyway)

A more general view of the question could be, how many strings are enough to stop concatenating and start building?

Should I even bother to test case the scenario?

if (myList.Count > 4) 
  ConcatWithStringBuilder(myList);
  • 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-12T19:17:47+00:00Added an answer on May 12, 2026 at 7:17 pm

    The usual answer is that string concatenation is more efficient for between 4 to 8 strings. It depends on whose blog you read.

    Don’t write a test to decide on which method to use. If you are unsure of whether it will go over the magic limit, then just use StringBuilder.

    Run this code to see the results for yourself:

    const int sLen=30, Loops=5000;
    DateTime sTime, eTime;
    int i;
    string sSource = new String('X', sLen);
    string sDest = "";
    // 
    // Time string concatenation.
    // 
    sTime = DateTime.Now;
    for(i=0;i<Loops;i++) sDest += sSource;
    eTime = DateTime.Now;
    Console.WriteLine("Concatenation took " + (eTime - sTime).TotalSeconds + " seconds.");
    // 
    // Time StringBuilder.
    // 
    sTime = DateTime.Now;
    System.Text.StringBuilder sb = new System.Text.StringBuilder((int)(sLen * Loops * 1.1));
    for(i=0;i<Loops;i++) sb.Append(sSource);
    sDest = sb.ToString();
    eTime = DateTime.Now;
    Console.WriteLine("String Builder took " + (eTime - sTime).TotalSeconds + " seconds.");
    // 
    // Make the console window stay open
    // so that you can see the results when running from the IDE.
    // 
    Console.WriteLine();
    Console.Write("Press Enter to finish ... ");
    Console.Read();
    

    Ref. http://support.microsoft.com/kb/306822

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

Sidebar

Related Questions

I have a situation where I need to populate a list object by looping
I have a situation where I need to put a url inside a GET
I have this situation where I need to get some data from a webpage
I have a situation where I need to store some data that just won't
I have a situation where I need to allocate people to several events. If
I have a situation where I need to extract a value from a form
I have a situation where I need to validate a form without actually having
I have a situation where I need to identify if a list element (li)
I have a situation where I need to databind a string array to a
I have situation where I need to change the order of the columns/adding new

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.