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

  • Home
  • SEARCH
  • 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 8542281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:01:30+00:00 2026-06-11T12:01:30+00:00

Meanwhile reading on the internet I learned that static variables always have the same

  • 0

Meanwhile reading on the internet I learned that static variables always have the same memory address. So when compiling the program, the compiler is going to decide what memory address to assign to the static variables. Reading that made me think about what happens when you do:

class Foo {}

class Program
{
    public static Foo someStaticVar;

    static void Main(string[] args)
    {
        Foo localVariable = new Foo();

        int x = 4;

        someStaticVar = localVariable; // is someStaticVariable still will have the same address?
    } 
    // variable x will be pushed of the stack
    // localVariable will be pushed of the stack
    // what happens then with someStatic var? 
}

I also learned that when declaring variables inside a method they will be pushed to the stack when created and popped out the stack when the method returns. If all that is true then someStaticVar should disappear but it doesn’t.

I am sure I must understood something incorrectly. Or maybe on the line someStaticVar = localVariable; is performing a deep copy of that object but I doubt it because there are a lot of questions on the interenet on how to do a deep copy of an object and they are much different than this approach.

  • 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-11T12:01:32+00:00Added an answer on June 11, 2026 at 12:01 pm

    In the example you provided localVariable is only a local variable to the Main method. It falls out of scope once the Main method ends executing. But since you have assigned it to the static field, the Foo instance that was created will continue to live outside of the Main method. And since it is static field it will live even outside of the Program class.

    So here’s what happens step by step:

    static void Main(string[] args)
    {
        // an instance of Foo is created and pushed on the heap
        // localVariable is now pointing to the address of this instance
        // localVariable itself is stored on the stack
        Foo localVariable = new Foo();
    
        // someStaticVar is now pointing to the same location on the heap as
        // the localVariable - the Foo instance created earlier
        someStaticVar = localVariable; 
    } 
    // at this stage the Main method finishes executing and all local
    // variables are falling out of scope. But since you have a static variable
    // pointing to the Foo instance that was created inside the Main method this
    // instance is not illegible for garbage collection because there are still 
    // references to it (someStaticVar).
    

    If someStaticVar was an instance field and not static then the same process will happen except that the instance will fall out of scope once there are no longer any references to the containing class (Program).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that wraps logic for matrix management. Meanwhile I have a
So in the meanwhile we know that double-checked-locking as is does not work in
Can I have a type (for now forgetting its semantics) that can be covariant
I found that wcslen() in VC++2010 returns correct count of letters; meanwhile Xcode does
I have a UISplit-view that uses Table-views in the left hand Master area and
I have a directory that continually fills up with artefact files. Many different programs
I'm learning C, but after that or in the meanwhile, what should I learn
I have a scenario where my application tries to connect to the server, meanwhile
Right now i have a mousedown event firing, meanwhile I would like to trigger
I have 3 listboxes and I want to scroll one meanwhile others also are

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.