Click here to Skip to main content
15,891,529 members
Home / Discussions / C#
   

C#

 
AnswerRe: How can you add a click event in the text box, C# , asp.net Pin
Pete O'Hanlon19-Jul-10 6:40
mvePete O'Hanlon19-Jul-10 6:40 
AnswerRe: How can you add a click event in the text box, C# , asp.net Pin
brunoseixas19-Jul-10 6:56
brunoseixas19-Jul-10 6:56 
QuestionDeclaring variables inside loops - inefficient? Pin
Adam Brown 319-Jul-10 5:19
Adam Brown 319-Jul-10 5:19 
AnswerRe: Declaring variables inside loops - inefficient? Pin
riced19-Jul-10 5:27
riced19-Jul-10 5:27 
AnswerRe: Declaring variables inside loops - inefficient? Pin
harold aptroot19-Jul-10 5:29
harold aptroot19-Jul-10 5:29 
AnswerRe: Declaring variables inside loops - inefficient? Pin
Ennis Ray Lynch, Jr.19-Jul-10 5:35
Ennis Ray Lynch, Jr.19-Jul-10 5:35 
AnswerRe: Declaring variables inside loops - inefficient? Pin
Luc Pattyn19-Jul-10 5:37
sitebuilderLuc Pattyn19-Jul-10 5:37 
AnswerRe: Declaring variables inside loops - inefficient? Pin
Keith Barrow19-Jul-10 5:44
professionalKeith Barrow19-Jul-10 5:44 
The first form (declaring the variable with the loop) is more efficient in terms of memory, the scope of the variable stays within the loop, so when the loop ends, the variable is ready for garbage collection. Declaring the variable before the loop increases its scope to the calling method (you might want this in some cases, but neither of the cases you described as like that), when the loop ends, the variable remains in scope until the calling method fishes.

Declaring the variable before the loop is also less readable, it also makes the code harder (or at least uglier) to refactor if, for example, you want to take the loop out into its own method.

Finally, and this is my opinion, I don't think you should worry too much about efficiency unless your method is used a lot. The things you need to do to make code efficient quite often makes it less readable (and harder to maintain). There was some quote like "prior optimisation is the thief of good code". Most code doesn't put that much stress on a server (unless you are looping/using it constantly, or the application has an inherently heavy workload, e.g.batch processing), and you should only really optimise code in such circumstances (and then really really optimise the most often executed code. That said, you also need to make sure your code doesn't become bloated, so it's always a bit of a balancing act.

Hope this answers your question.

[Edit]
I'm waiting to be flamed now....

ragnaroknrol The Internet is For Porn[^]

Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

GeneralRe: Declaring variables inside loops - inefficient? Pin
Adam Brown 319-Jul-10 5:53
Adam Brown 319-Jul-10 5:53 
GeneralRe: Declaring variables inside loops - inefficient? Pin
harold aptroot19-Jul-10 5:54
harold aptroot19-Jul-10 5:54 
GeneralRe: Declaring variables inside loops - inefficient? [modified] Pin
Keith Barrow19-Jul-10 6:05
professionalKeith Barrow19-Jul-10 6:05 
GeneralRe: Declaring variables inside loops - inefficient? Pin
harold aptroot19-Jul-10 6:18
harold aptroot19-Jul-10 6:18 
GeneralRe: Declaring variables inside loops - inefficient? Pin
Keith Barrow19-Jul-10 6:35
professionalKeith Barrow19-Jul-10 6:35 
GeneralRe: Declaring variables inside loops - inefficient? Pin
harold aptroot19-Jul-10 6:52
harold aptroot19-Jul-10 6:52 
GeneralRe: Declaring variables inside loops - inefficient? Pin
Luc Pattyn19-Jul-10 6:52
sitebuilderLuc Pattyn19-Jul-10 6:52 
GeneralRe: Declaring variables inside loops - inefficient? Pin
Pete O'Hanlon19-Jul-10 6:54
mvePete O'Hanlon19-Jul-10 6:54 
AnswerRe: Declaring variables inside loops - inefficient? Pin
Luc Pattyn19-Jul-10 7:04
sitebuilderLuc Pattyn19-Jul-10 7:04 
Questionreading weather.com data feed Pin
Jassim Rahma19-Jul-10 5:07
Jassim Rahma19-Jul-10 5:07 
AnswerRe: reading weather.com data feed Pin
Eddy Vluggen19-Jul-10 5:20
professionalEddy Vluggen19-Jul-10 5:20 
AnswerRe: reading weather.com data feed Pin
brunoseixas19-Jul-10 6:58
brunoseixas19-Jul-10 6:58 
QuestionRtlSetProcessIsCritical, Marshall and pointers. Pin
Night Lol19-Jul-10 4:45
Night Lol19-Jul-10 4:45 
AnswerRe: RtlSetProcessIsCritical, Marshall and pointers. Pin
Luc Pattyn19-Jul-10 5:00
sitebuilderLuc Pattyn19-Jul-10 5:00 
GeneralRe: RtlSetProcessIsCritical, Marshall and pointers. Pin
Night Lol19-Jul-10 5:14
Night Lol19-Jul-10 5:14 
GeneralRe: RtlSetProcessIsCritical, Marshall and pointers. Pin
DaveyM6919-Jul-10 5:31
professionalDaveyM6919-Jul-10 5:31 
GeneralRe: RtlSetProcessIsCritical, Marshall and pointers. Pin
Night Lol19-Jul-10 5:51
Night Lol19-Jul-10 5:51 

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.