string or Stringbuilder??

A

ALI-R

Shall I use stringbuilder in the following statement? why?

string devInfo =
"<DeviceInfo>" +
"<Toolbar>False</Toolbar>" +
"<MarginLeft>0in</MarginLeft>" +
"<MarginRight>0in</MarginRight>" +
"<MarginTop>0in</MarginTop>" +
"<MarginBottom>0in</MarginBottom>" +
"<PageHeight>" +
myVariable1.toString() +
"</PageHeight>" +
"<PageWidth>" +
myVariable2.toString();

thanks for your help.
Ali
 
P

Peter Rilling

Is this inside of a loop?

If this is executed once, a StringBuilder might not be necessary as the
performance gain might not be substantial. Although there are times when
StringBuilder are beneficial, they may not always be necessary.
 
B

Bret Pehrson

ALI-R said:
Shall I use stringbuilder in the following statement? why?

string devInfo =
"<DeviceInfo>" +
"<Toolbar>False</Toolbar>" +
"<MarginLeft>0in</MarginLeft>" +
"<MarginRight>0in</MarginRight>" +
"<MarginTop>0in</MarginTop>" +
"<MarginBottom>0in</MarginBottom>" +
"<PageHeight>" +
myVariable1.toString() +
"</PageHeight>" +
"<PageWidth>" +
myVariable2.toString();

thanks for your help.
Ali

String should be just fine. StringBuilder is good when you are adding to the
string and/or otherwise causing it to resize numerous times (e.g. in a loop).
 
J

Jon Skeet [C# MVP]

ALI-R said:
Shall I use stringbuilder in the following statement? why?

string devInfo =
"<DeviceInfo>" +
"<Toolbar>False</Toolbar>" +
"<MarginLeft>0in</MarginLeft>" +
"<MarginRight>0in</MarginRight>" +
"<MarginTop>0in</MarginTop>" +
"<MarginBottom>0in</MarginBottom>" +
"<PageHeight>" +
myVariable1.toString() +
"</PageHeight>" +
"<PageWidth>" +
myVariable2.toString();

No - that ends up becoming one single String.Concat call with four
parameters - a single constant string "<DeviceInfo><Toolbar>[...]",
myVariable1.ToString(), "</PageHeight><PageWidth>" and
myVariable2.ToString().
 
A

ALI-R

No,it is not in the loop.

Peter Rilling said:
Is this inside of a loop?

If this is executed once, a StringBuilder might not be necessary as the
performance gain might not be substantial. Although there are times when
StringBuilder are beneficial, they may not always be necessary.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top