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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:00:53+00:00 2026-06-06T02:00:53+00:00

In my project I am looping across a dataview result. string html =string.empty; DataView

  • 0

In my project I am looping across a dataview result.

 string html =string.empty;
 DataView dV = data.DefaultView;
 for(int i=0;i< dV.Count;i++)
 {
     DataRowView rv = dV[i];
     html += rv.Row["X"].Tostring();
 }

Number of rows in dV will alway be 3 or 4.

Is it better to use the string concat += opearator or StringBuilder for this case and why?

  • 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-06T02:00:55+00:00Added an answer on June 6, 2026 at 2:00 am

    I would use StringBuilder here, just because it describes what you’re doing.

    For a simple concatenation of 3 or 4 strings, it probably won’t make any significant difference, and string concatenation may even be slightly faster – but if you’re wrong and there are lots of rows, StringBuilder will start getting much more efficient, and it’s always more descriptive of what you’re doing.

    Alternatively, use something like:

    string html = string.Join("", dv.Cast<DataRowView>()
                                    .Select(rv => rv.Row["X"]));
    

    Note that you don’t have any sort of separator between the strings at the moment. Are you sure that’s what you want? (Also note that your code doesn’t make a lot of sense at the moment – you’re not using i in the loop. Why?)

    I have an article about string concatenation which goes into more detail about why it’s worth using StringBuilder and when.

    EDIT: For those who doubt that string concatenation can be faster, here’s a test – with deliberately “nasty” data, but just to prove it’s possible:

    using System;
    using System.Diagnostics;
    using System.Text;
    
    class Test
    {
        static readonly string[] Bits = { 
            "small string",
            "string which is a bit longer",
            "stirng which is longer again to force yet another copy with any luck"
        };
    
        static readonly int ExpectedLength = string.Join("", Bits).Length;
    
        static void Main()        
        {
            Time(StringBuilderTest);
            Time(ConcatenateTest);
        }
    
        static void Time(Action action)
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            // Make sure it's JITted
            action();
            Stopwatch sw = Stopwatch.StartNew();
            for (int i = 0; i < 10000000; i++)
            {
                action();
            }
            sw.Stop();
            Console.WriteLine("{0}: {1} millis", action.Method.Name,
                              (long) sw.Elapsed.TotalMilliseconds);
        }
    
        static void ConcatenateTest()
        {
            string x = "";
            foreach (string bit in Bits)
            {
                x += bit;
            }
            // Force a validation to prevent dodgy optimizations
            if (x.Length != ExpectedLength)
            {
                throw new Exception("Eek!");
            }
        }
    
        static void StringBuilderTest()
        {
            StringBuilder builder = new StringBuilder();
            foreach (string bit in Bits)
            {
                builder.Append(bit);
            }
            string x = builder.ToString();
            // Force a validation to prevent dodgy optimizations
            if (x.Length != ExpectedLength)
            {
                throw new Exception("Eek!");
            }
        }
    }
    

    Results on my machine (compiled with /o+ /debug-):

    StringBuilderTest: 2245 millis
    ConcatenateTest: 989 millis
    

    I’ve run this several times, including reversing the order of the tests, and the results are consistent.

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

Sidebar

Related Questions

I am working on android project and I am looping through each control within
I need to parse html for a project and looking for a good html
In a project I've been trying to familiarise myself with, I ran across a
We have an object model being used across three applications. Two programs collect data,
I have a table representing values of source file metrics across project revisions, like
Members at work have come across a C# project posted on Google code that
My latest idea for do settings across my php project I am building was
In a project I'm currently working on, we send some small info across the
For my project I am looking for a JAVA library that can help me
In this Java project I'm looking at, I keep seeing code with HashMap, like

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.