Click here to Skip to main content
15,884,425 members
Home / Discussions / C#
   

C#

 
GeneralRe: Linq Query Problem Pin
Kevin Marois11-Aug-10 5:44
professionalKevin Marois11-Aug-10 5:44 
GeneralAmazing Pin
Ennis Ray Lynch, Jr.11-Aug-10 7:04
Ennis Ray Lynch, Jr.11-Aug-10 7:04 
GeneralRe: Amazing Pin
Kevin Marois11-Aug-10 7:06
professionalKevin Marois11-Aug-10 7:06 
QuestionStringBuilder vs += on string Pin
Dewald11-Aug-10 4:16
Dewald11-Aug-10 4:16 
AnswerRe: StringBuilder vs += on string Pin
Eddy Vluggen11-Aug-10 4:19
professionalEddy Vluggen11-Aug-10 4:19 
GeneralRe: StringBuilder vs += on string PinPopular
Ian Shlasko11-Aug-10 4:31
Ian Shlasko11-Aug-10 4:31 
AnswerRe: StringBuilder vs += on string Pin
Nuri Ismail11-Aug-10 4:26
Nuri Ismail11-Aug-10 4:26 
AnswerRe: StringBuilder vs += on string [modified] Pin
Luc Pattyn11-Aug-10 4:30
sitebuilderLuc Pattyn11-Aug-10 4:30 
it typically is (a lot) faster. When you have:
string[] lines=...;
string s="";
foreach(string line in lines) s+=line;


each iteration of the for loop will create a new string object and copy all data so far into it.

OTOH
string[] lines=...;
StringBuilder sb=new StringBuilder();
foreach(string line in lines) sb.Append(line);
string s=sb.ToString();


will use only two objects (sb and s), and sb.Append will copy the new data, not all the data, provided the internal buffer has sufficient capacity. The StringBuilder's capacity by default starts out at 16 characters, and doubles when necessary.
You can avoid all unnecessary capacity enlargements and data copies by providing an appropriate initiaal caapacity, so the best approach could be:
string[] lines=...;
StringBuilder sb=new StringBuilder(1000);
foreach(string line in lines) sb.Append(line);
string s=sb.ToString();

which starts out at an SB with room for 1000 characters (which would grow if required, just as before).

FYI: it does not make sense to use StringBuilder for concatenating string literals, as the compiler does that for you automaticaally. So
string s="aaaa"+"bbbbbbbbbbbb"+"ccccc";
only takes one object and one assignment.

ADDED
.NET has some methods that also help in improving string concatenation performance, without any hassle.
string.Join() is a good example.
/ADDED

Smile | :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.


modified on Wednesday, August 11, 2010 11:13 AM

AnswerRe: StringBuilder vs += on string Pin
Richard Blythe11-Aug-10 4:38
Richard Blythe11-Aug-10 4:38 
AnswerRe: StringBuilder vs += on string Pin
Ennis Ray Lynch, Jr.11-Aug-10 4:43
Ennis Ray Lynch, Jr.11-Aug-10 4:43 
JokeRe: StringBuilder vs += on string Pin
Not Active11-Aug-10 9:08
mentorNot Active11-Aug-10 9:08 
GeneralRe: StringBuilder vs += on string Pin
Ennis Ray Lynch, Jr.11-Aug-10 9:35
Ennis Ray Lynch, Jr.11-Aug-10 9:35 
GeneralRe: StringBuilder vs += on string Pin
Luc Pattyn11-Aug-10 9:45
sitebuilderLuc Pattyn11-Aug-10 9:45 
JokeThe irony Pin
Ennis Ray Lynch, Jr.11-Aug-10 10:11
Ennis Ray Lynch, Jr.11-Aug-10 10:11 
GeneralRe: The irony Pin
Luc Pattyn11-Aug-10 10:17
sitebuilderLuc Pattyn11-Aug-10 10:17 
AnswerRe: StringBuilder vs += on string Pin
AspDotNetDev11-Aug-10 9:22
protectorAspDotNetDev11-Aug-10 9:22 
QuestionMultiple report system Pin
Neo Maximus11-Aug-10 4:14
Neo Maximus11-Aug-10 4:14 
AnswerRe: Multiple report system Pin
Chris Trelawny-Ross11-Aug-10 8:19
Chris Trelawny-Ross11-Aug-10 8:19 
QuestionSystem.Globalization.CultureInfo [modified] Pin
ONeil Tomlinson11-Aug-10 3:55
ONeil Tomlinson11-Aug-10 3:55 
AnswerRe: System.Globalization.CultureInfo Pin
Dewald11-Aug-10 4:09
Dewald11-Aug-10 4:09 
AnswerRe: System.Globalization.CultureInfo Pin
Ian Shlasko11-Aug-10 4:09
Ian Shlasko11-Aug-10 4:09 
QuestionConstraints taking either types in generics Pin
dashingsidds11-Aug-10 3:34
dashingsidds11-Aug-10 3:34 
AnswerRe: Constraints taking either types in generics Pin
Ennis Ray Lynch, Jr.11-Aug-10 3:52
Ennis Ray Lynch, Jr.11-Aug-10 3:52 
AnswerRe: Constraints taking either types in generics Pin
kapax511-Aug-10 3:52
kapax511-Aug-10 3:52 
GeneralRe: Constraints taking either types in generics Pin
dashingsidds11-Aug-10 4:00
dashingsidds11-Aug-10 4:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.