Optimizer Query

  • Thread starter Thread starter C# Learner
  • Start date Start date
C

C# Learner

With code like this:

public void SomeMethod()
{
for (int i = 0; i < SomeLargeNumber; ++i) {
int n = GetANumber();
DoSomethingWith(n);
}
}

Would the "int n" part be optimized out somewhere along the line?
i.e.: would something like the following happen instead:

public void SomeMethod()
{
int n;

for (int i = 0; i < SomeLargeNubmer; ++i) {
n = GetANumber();
DoSomethingWith(n);
}
}

Just curious...
 
It will generate the same code, or at least one equivalent.

If what you are worried about is that each time the loop is executed a new
instance is created fear not, it does not happen, the variable n is created
in the stack and is created only once,


Hope this help,
 
"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us said:
It will generate the same code, or at least one equivalent.

If what you are worried about is that each time the loop is executed a new
instance is created fear not, it does not happen, the variable n is created
in the stack and is created only once,


Hope this help,

Thankyou.
 
Back
Top