This may be a very simple question, but I could not find the answer here on SO, and did not know who I asked to answer:
I can write a simple C # method as follows:
private void foo() { int a = 1; int b = 5; }
If the CIL code (generated by the compiler) is executed using the Common Language Runtime, it will create the following fields on top of the stack, while the executive control is inside the method:
b = 5 a = 1
But now I am expanding a method to access a field called "a":
private void foo() { int a = 1; int b = 5; Console.WriteLine(a); }
Now the CLR must access a field that is not on top of the stack, but in accordance with the FILO principle (first in last), it must take care of all the fields over the requested fields before accessing it.
What happens to field "b", which is on the stack above the requested field "a"?
It is not possible to delete it, since it can be used by the execution method after that, and what happens to it?
AFAIK, there are only two ways to store a field, stack, or heap. Moving it to a heap does not make much sense, since it will require all the benefits of stacking from the CLR. Does the CLR create something like a second stack?
How it works?
-edit -
Perhaps I did not explain my intentions 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 to the stack and accesses them later, but in the reverse order.
First, he must gain access to field “a,” but to get to it, the CLR must take care of field “b”, which lies above field “a” on the stack. He cannot simply remove field “b” from the stack, since he must access it later.
How it works?