Re: Unsure about Something

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Richard A. Lowe (chadich_at_yumspamyumYahoo.com)
Date: 02/16/04


Date: Sun, 15 Feb 2004 22:33:10 -0600

Well, form a purely IL (intermediate language) perspective the variable 'n'
IS being created with a *method-wide* scope level.

The simple example:
 for (int i = 0; i < 10; ++i) {
 int n = i;
 }

produces the following relevant IL:
.method private hidebysig static void Main(string[] args) cil managed
{
  .entrypoint
  .maxstack 2
  .locals init ([0] int32 i,
           [1] int32 n)

The ".locals init" line defines two int32's at the start of the method.
Indeed, I don't think there is any other way to do this (declare a local
variable) in IL - so enforcing scope more granular than method level is a
job for the compiler.

Richard

-- 
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"C# Learner" <csharp@learner.here> wrote in message
news:fmf0301n6pslqsnr4gf19uo2gbbp3dt61d@4ax.com...
> "Bjorn Abelli" <bjorn_abelli@DoNotSpam.hotmail.com> wrote:
>
> <snip>
>
> >> Okay, but does _a_ get re-created each time the "for (int j..." loop
> >> is entered, or is it created only once, when SomeMethod() is entered?
> >
> >In each iteration you have declared a new variable with the name "a".
This
> >variable is "destroyed" for each turn, so in that sense you can say that
> >it's "re-created" (though it's not the same variable, but a "new"
variable
> >for each iteration).
>
> Hi,
>
> I've previously tried Googling on this subject and have found that
> some say the above is true and some say the opposite is true.
>
> i.e.:
>
> for (int i = 0; i < 10; ++i) {
> int n = i;
> }
>
> Some say that _n_ is only *created* once, whereas others say that it
> is re-created on each iteration of the loop.
>
> So in the former case, the integer variable _n_ would be created once.
> In the latter, 10 integer variables would eventually be created; one
> for each iteration of the loop.
>
> I'm still not sure which side to believe...
>
> I'd like to use the whole declaration inside the loop, for
> readability, instead of declaring the int outside the loop once then
> using it in the loop.  However, if this is going to cost in terms of
> performance I wouldn't want to do it.
>
> >// Bjorn A


Relevant Pages

  • Re: Local variables within looping constructs
    ... > the iteration. ... variable like an int there may well be no cost at all. ... That isn't within the loop, though, that is exactly equivalent to ... variables local to where they are used inproves clarity and reduces the ...
    (comp.lang.c)
  • SQL 2000 Loop to send Email
    ... then how do I get the loop to work. ... (mailid int,name varchar (20),email varchar,case varchar ... declare @autonum int ...
    (microsoft.public.sqlserver.programming)
  • Re: Preprocessor unique names
    ... the for loop. ... This means that my macro must declare a variable in an ... Anyway, your macro looks bogus, since p doesn't change during iteration ...
    (comp.lang.c)
  • Correlated Subqueries?
    ... I need to loop through several rows and find matches for values and the ... DECLARE @ParentID int ... Isn't the correlated sub query supposed to return more than one value so you ...
    (microsoft.public.sqlserver.server)
  • Re: Unsure about Something
    ... In the second case you declare a *new* variable in each iteration. ... Because you've gone "out of scope" for the loop, ...
    (microsoft.public.dotnet.languages.csharp)