Declaring variables inside-versus-outside loops

Monday, 10 May 2010 11:39 GMT | 2 comments

IN .NET, there is no difference between declaring variables inside loops and declaring variables outside loops. Here is the proof.

Here are two programs inside.cs and outside.cs, appropriately named to reflect the two ways respectively.

inside.cs versus outside.cs

And, here is the ILASM obtained by disassembling inside.exe and outside.exe.

ildasm output of inside.exe and outside.exe

As you can see, the two are the same.

So, please leave those variables inside loops when they are not needed outside. Having the variables closer to their point of use makes the code easier to read.

Eddy.

2 Comments

RSS feed for comments on this post. TrackBack URI

  1. cannot find symbol method println java.lang.string

    please help

    Comment by shehzu — Tuesday, 9 November 2010 15:41 GMT #

  2. Uh? :)

    Comment by Eddy — Saturday, 13 November 2010 17:07 GMT #

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Powered by WordPress and Eddy Young.

DISCLAIMER: This site is supported by advertising. As a result, cookies may be installed by advertisers in order to track usage and trends. If you do not want this, please disable cookies for this site.