简体   繁体   中英

C# StringBuilder way to build string with sessions

I have such piece of code how I'm building string:

foreach (var item in Session)
{
    StringBuilder sb = new StringBuilder();
    sb.Append("Session Parameter: [" + item.ToString() + "]<p />Guid Value: [" + Session[item.ToString()] + "]");
    Response.Write(sb.ToString());
}

I wonder, is such way correct? I mean, is StringBuilder here a very useful stuff?

And is this code faster without StringBuilder ?

If it was built with the additional operators as Label1.Text += ...

As I remember += uses more operations than StringBuilder as:

  • making temp variable ( copy ) from the original string
  • then changing the value of copy
  • then redefining the original value with the value of copy

Why not create the StringBuilder once and use a single Response.Write ?:

StringBuilder sb = new StringBuilder();
foreach (string item in Session)
{
    sb.Append("Session Parameter: [");
    sb.Append(item.ToString());
    sb.Append("]<p />Guid Value: [");
    sb.Append(((string)Session[item]));        
    sb.Append(']');
}
Response.Write(sb.ToString());

You should benchmark. I don't think there will be much difference unless the loop is very large.

Whenever you have concerns about performance, you should benchmark. The code that could get the greatest benefit from optimisation is not always where you would think it to be.

You probably want to restructure your code like this:

StringBuilder sb = new StringBuilder();
foreach (var item in Session)
{
    sb.Append("Session Parameter: [");
    sb.Append(item.ToString());
    sb.Append("]<p />Guid Value: [");
    sb.Append(Session[item.ToString()]);
    sb.Append("]");
}
Response.Write(sb.ToString());

You only need one StringBuilder and one Response.Write . Putting them in the loop defeats any optimization you might get from using the StringBuilder and there is no reason to do a Response.Write more than once.

You're not getting the benefits of StringBuilder because you are still concatenating your strings with + . A version optimized for StringBuilder would be:

foreach (var item in Session)
{
    StringBuilder sb = new StringBuilder();
    sb.Append("Session Parameter: [");
    sb.Append(item.ToString());
    sb.Append("]<p />Guid Value: [");
    sb.Append(Session[item.ToString()]);
    sb.Append("]");

    Response.Write(sb.ToString());
}

Note: you can also move the StringBuilder initialization and call to Response.Write outside of the foreach as another possible improvement.

You should change your code as follows:

sb.Append("Session Parameter: [");
sb.Append(item); // ToString is implicit
sb.Append("]<p />Guid Value: [");
sb.Append(Session[item.ToString()]);
sb.Append("]");

Otherwise, the string builder is not that useful: all you do is appending a single value, which is constructed with an implicitly constructed builder created by the compiler for you.

The most important thing is moving the string builder and writing the response outside the loop.

Response.Write is more than enough for this task. String builder makes it slower.

foreach (var item in Session)
{
    Response.Write("Session Parameter: [");
    Response.Write(item.ToString());
    Response.Write("]<p />Guid Value: [");
    Response.Write(Session[item.ToString()]);        
    Response.Write(']');
}

Take a look at http://www.dotnetperls.com/response-write

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM