Click here to Skip to main content
15,886,199 members
Home / Discussions / C#
   

C#

 
QuestionLinq Query Problem Pin
Kevin Marois11-Aug-10 4:59
professionalKevin Marois11-Aug-10 4:59 
AnswerRe: Linq Query Problem Pin
Ennis Ray Lynch, Jr.11-Aug-10 5:38
Ennis Ray Lynch, Jr.11-Aug-10 5:38 
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 
The real difference is in allocations... a += b is really shorthand for a = a + b, which works by creating a new string of the proper length, copying a and b to it, and assigning that value to a.

If you do that a thousand times, you're allocating a thousand strings in memory, each one a bit longer than the previous.

The StringBuilder, if I remember correctly, basically works like a variable-length array. If it starts with a length of 100, then you can just keep appending text in-place until it reaches a length of 100, at which point it'll reallocate itself to a length of, say, 200. Instead of doing a thousand allocations, you might only be doing a hundred or less. If you already know something about how many strings you'll be adding, or their average length, you can tweak the StringBuilder in its constructor to reduce this further.

It's a minor improvement if you're only adding a few things, but it really makes a huge difference in a long loop, or one that you repeat often.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)

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 
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 

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.