Re: Unsure about Something
From: Richard A. Lowe (chadich_at_yumspamyumYahoo.com)
Date: 02/16/04
- Next message: kids: "Re: make flexgrid scrollable"
- Previous message: Alvin Bruney [MVP]: "Re: Session Variables Disappearing"
- In reply to: C# Learner: "Re: Unsure about Something"
- Next in thread: C# Learner: "Re: Unsure about Something"
- Reply: C# Learner: "Re: Unsure about Something"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: kids: "Re: make flexgrid scrollable"
- Previous message: Alvin Bruney [MVP]: "Re: Session Variables Disappearing"
- In reply to: C# Learner: "Re: Unsure about Something"
- Next in thread: C# Learner: "Re: Unsure about Something"
- Reply: C# Learner: "Re: Unsure about Something"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|