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 4085780
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:37:43+00:00 2026-05-20T18:37:43+00:00

I’m a little confused about how ValueType objects are managed memory-wise in .NET. I

  • 0

I’m a little confused about how ValueType objects are managed memory-wise in .NET. I get that value types are put on the stack for each instance/declaration. I’m not quite sure about what constitutes a new instance/declaration. I think my main question is:

If I pass a ValueType parameter to a method, but do not assign it a value, then is a new instance created on the stack?

I’m not sure if I am phrasing this correctly, so I wrote a simple sample program to make it easier to understand my confusion.

void Main()
{
    //-------------------------|  Line 1:
    int MichaelJordan = 23;      
    //-------------------------|  Line 2:   
    int HoursInADay = 24;           
    //-------------------------|  Line 3:
    Console.WriteLine("Michael Jordan is "+(NumberHelper.Is23(MichaelJordan) ? string.Empty : "not ")+"23");
    //-------------------------|  Line 4:
    Console.WriteLine("The last number that wasn't 23 was "+NumberHelper.LastNumberThatWasnt23);
    //-------------------------|  Line 5:
    Console.WriteLine("Hours in a Day is "+(NumberHelper.Is23(HoursInADay) ? string.Empty : "not ")+"23");
    //-------------------------|  Line 6:  
    Console.WriteLine("The last number that wasn't 23 was "+NumberHelper.LastNumberThatWasnt23);
}

public static class NumberHelper {  
    public static int LastNumberThatWasnt23;
    public static bool Is23(int candidate){
        if(candidate != 23){
            LastNumberThatWasnt23 = candidate;
            return false;
        }
        return true;
    }
}

So, here are my questions with respect to the program.

  1. How many integer instances are created on the stack in this program?

  2. Where in the code, exactly, is each value type instance created on the stack?

Although there is a good chance I am wrong, this is what I think happens, and what I’m not sure about.

  • Line 1: I know that a new instance created on the stack for variable MichaelJordan

  • Line 2: I know that a new instance created on the stack for variable HoursInADay

  • Line 3: Is a copy of the value instance from the variable MichaelJordan created in
    order to be a method parameter? Is an instance created of the value 23 for the comparison?

  • Line 4: The value for the LastNumberThatWasnt23 field is not initialized, so it returns the default value. Is a new instance with the default value created for this field?

  • Line 5: Same questions as Line 3, but also, I know that a new instance is created on the stack for the field LastNumberThatWasnt23 because I am setting it.

  • Line 6: Now, the field has a value. But, in referencing that value and passing it into the Console.WriteLine method, am I creating a new value instance on the stack?

I have one more part to this question, which is How would I find out stuff like this? Can I do it programmatically? Are there any tools that can be used to reveal this kind of information?

I know this is a long question, so I thank you for taking the time to read it.

  • 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-05-20T18:37:44+00:00Added an answer on May 20, 2026 at 6:37 pm

    This depends on the jitter your machine uses to run this program. But since you use only two local variables, it is very likely the correct answer is zero. The jit optimizer stores the variable values in CPU registers. That’s one of the standard optimizations.

    You’ll find more about the kind of optimizations that the jitter performs in this answer.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I am trying to loop through a bunch of documents I have to put
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and

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.