this might be a very simple question, but I could not find an answer here on SO nor knew anyone I asked an answer:
I can write an easy c# method like this:
private void foo()
{
int a = 1;
int b = 5;
}
If the CIL-Code (created by the compiler) gets executed by the Common Language Runtime, it will create the following fields on top of the stack while the executing control is inside the method:
b = 5
a = 1
But now, I extend the Method to access the field called "a" to this:
private void foo()
{
int a = 1;
int b = 5;
Console.WriteLine(a);
}
Now the CLR has to access a field which is not on top of the stack, but according to the FILO (first in, last out) principle, it has to take care of all fields above the requested fields before accessing it.
What happens to the field called "b" which is on the stack above the requested field "a"?
The CLR cant delete it, as it might be used by the executing method afterwards, so what happens to it?
AFAIK, there are only 2 ways to store a field, stack or heap. Moving it to the heap would'nt make much sense as this would take all the benefits from stacking from the CLR. Does the CLR create something like a second stack?
How does that work exactly?
-edit-
Maybe I didn't explain my intentions clear enough.
If i write a Method like this:
private void foo()
{
int a = 1;
int b = 5;
Console.WriteLine(a);
Console.WriteLine(b);
}
The CLR first writes 2 fields on the stack and accesses them afterwards, but in reversed order.
First, it has to access field "a", but to get to it, the CLR has to take care of field "b" which lies above field "a" on the stack. It cant just remove field "b" from the stack as it has to access it afterwards.
How does that work?
Please note that, while you're talking about fields, a
and b
are called local variables.
Maybe the following simplified logical representation can clear up things. Before the call to Console.WriteLine
, the top of the stack would look something like this:
|5| // b
|1| // a
Inside Console.WriteLine
, an additional stackframe is added for its parameter (called value
, which gets a copy of the variable a
):
|1| // value = a
|5| // b
|1| // a
Once Console.WriteLine returns, the top frame is popped and the stack becomes again:
|5| // b
|1| // a