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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:49:31+00:00 2026-05-10T13:49:31+00:00

public class MyClass { public int Age; public int ID; } public void MyMethod()

  • 0
public class MyClass {     public int Age;     public int ID; }  public void MyMethod()  {     MyClass m = new MyClass();     int newID; } 

To my understanding, the following is true:

  1. The reference m lives on the stack and goes out of scope when MyMethod() exits.
  2. The value type newID lives on the stack and goes out of scope when MyMethod() exits.
  3. The object created by the new operator lives in the heap and becomes reclaimable by the GC when MyMethod() exits, assuming no other reference to the object exists.

Here is my question:

  1. Do value types within objects live on the stack or the heap?
  2. Is boxing/unboxing value types in an object a concern?
  3. Are there any detailed, yet understandable, resources on this topic?

Logically, I’d think value types inside classes would be in the heap, but I’m not sure if they have to be boxed to get there.

Edit:

Suggested reading for this topic:

  1. CLR Via C# by Jeffrey Richter
  2. Essential .NET by Don Box
  • 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. 2026-05-10T13:49:32+00:00Added an answer on May 10, 2026 at 1:49 pm

    Value-type values for a class have to live together with the object instance in the managed heap. The thread’s stack for a method only lives for the duration of a method; how can the value persist if it only exists within that stack?

    A class’ object size in the managed heap is the sum of its value-type fields, reference-type pointers, and additional CLR overhead variables like the Sync block index. When one assigns a value to an object’s value-type field, the CLR copies the value to the space allocated within the object for that particluar field.

    Take for example, a simple class with a single field.

    public class EmbeddedValues {   public int NumberField; } 

    And with it, a simple testing class.

    public class EmbeddedTest {   public void TestEmbeddedValues()   {     EmbeddedValues valueContainer = new EmbeddedValues();      valueContainer.NumberField = 20;     int publicField = valueContainer.NumberField;   } } 

    If you use the MSIL Disassembler provided by the .NET Framework SDK to peek at the IL code for EmbeddedTest.TestEmbeddedValues()

    .method public hidebysig instance void  TestEmbeddedValues() cil managed {   // Code size       23 (0x17)   .maxstack  2   .locals init ([0] class soapextensions.EmbeddedValues valueContainer,            [1] int32 publicField)   IL_0000:  nop   IL_0001:  newobj     instance void soapextensions.EmbeddedValues::.ctor()   IL_0006:  stloc.0   IL_0007:  ldloc.0   IL_0008:  ldc.i4.s   20   IL_000a:  stfld      int32 soapextensions.EmbeddedValues::NumberField   IL_000f:  ldloc.0   IL_0010:  ldfld      int32 soapextensions.EmbeddedValues::NumberField   IL_0015:  stloc.1   IL_0016:  ret } // end of method EmbeddedTest::TestEmbeddedValues 

    Notice the CLR is being told to stfld the loaded value of ’20’ in the stack to the loaded EmbeddValues’ NumberField field location, directly into the managed heap. Similarly, when retrieving the value, it uses ldfld instruction to directly copy the value out of that managed heap location into the thread stack. No box/unboxing happens with these types of operations.

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

Sidebar

Ask A Question

Stats

  • Questions 146k
  • Answers 146k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Both Entity SQL and LINQ expressions are parsed into a… May 12, 2026 at 8:57 am
  • Editorial Team
    Editorial Team added an answer There are two problems in your code the GridViewColumn is… May 12, 2026 at 8:57 am
  • Editorial Team
    Editorial Team added an answer Percents uploaded=100/file size*bytes uploaded remember to use the same units… May 12, 2026 at 8:57 am

Related Questions

Let's suppose I have this object: [Serializable] public class MyClass { public int Age
I have a class: public class MyClass { public int code { set; get;
I'm writing some code for a class constructor which loops through all the properties
I've seen this is various codebases, and wanted to know if this generally frowned

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.