Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8311761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:55:05+00:00 2026-06-08T19:55:05+00:00

this might be a very simple question, but I could not find an answer

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:55:06+00:00Added an answer on June 8, 2026 at 7:55 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might be a very simple question, but I could not find the answer
This question might appear very simple, but i haven't found an answer yet, so
This might be a very newbie question, but I didn't find anything satisfying I
I know, this might be a very basic question but I am not 100%
This might be a very simple question but i am unable to get it.
Ok, this might be a very noob question, but I find that PHP Documentation
This might seem like a very simple question, but I am struggling with it.
This might be a very simple question but after much searching on different forums
I know it might be a very simple solution for this, but I am
This might be a very basic question but it confuses me. Can two different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.