Share via

If class variable stored in stack and object in the managed heap then where the class properties get stored in C# .NET?

Jairaj Kushwaha 0 Reputation points
2026-05-28T17:35:32.5833333+00:00

If I create an Employee class and this class have 2 members EmployeeId and EmployeeName so where these properties and it's value get stored either in managed heap or stack or both in C# .NET?

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 84,066 Reputation points
    2026-05-29T15:19:42.69+00:00

    reference types are allocated on the heap, value types are typically allocated on the stack, but boxing (cast as object) will allocate on the heap. if a class instance contains a value type, its allocated with the instance heap allocation in-place not separately. a class instance with a reference type (say a string), the pointer is in the instance allocation, and the the string instance is a separate heap allocation.

    to better understand, if a variable is a value type, the variable references the actual value, which if a local variable is allocated on the stack. a static value type variable is allocated in the compile data space. reference type variables are a pointer to the object instance. again if a local variable, the pointer is allocated on the stack, if static the compiled data space.

    at the compiler level, a local variable is a stack pointer address plus an offset. if the variable is a value type the computed address contains the value, if a reference type, its the pointer to the object instance.

    Was this answer helpful?

    0 comments No comments

  2. Taki Ly (WICLOUD CORPORATION) 1,585 Reputation points Microsoft External Staff Moderator
    2026-05-29T06:03:37.3766667+00:00

    Hello @Jairaj Kushwaha ,

    Thank you for raising a question about memory management in C# .NET. I built a small reproduce to visually demonstrate where class variables and object data are stored in memory.

    User's image

    Case 1: Stack vs Heap

    [STACK] 'emp' reference address : 0xA5CDFEEBA8   <- Stack
    [HEAP]  Employee object address : 0x2788A107470  <- Heap
    Same address?                   : False
    

    The reference variable emp lives on the Stack, while the actual Employee object lives on the Managed Heap at a completely different address.

    Case 2: int (inline) vs string (separate) inside Employee

    [HEAP]  Employee object address          : 0x2788A107470
    [HEAP]  EmployeeName (string) address    : 0x2788000C1C0  <- different!
    Same address?                            : False
    [INLINE]   EmployeeId   = 101    (int, stored directly inside Employee object)
    [SEPARATE] EmployeeName = "John" (string, stored separately on heap)
    

    Not all properties are stored the same way. EmployeeId (int) is stored inside the Employee object, value types are stored inline. EmployeeName (string) is a separate object on the heap, the Employee object only holds a reference to it.

    Below are Official Microsoft Documentations:

    C# Classes

    Properties (C# Programming Guide)

    Hope this clears things up. If my explanation and the information I provided were helpful, I would greatly appreciate it if you could follow the instructions here so others with the same problem can benefit as well.

    Thank you.

    Was this answer helpful?

    0 comments No comments

  3. AI answer

    2026-05-28T17:35:46.51+00:00

    For a C# class, the properties’ data is stored as part of the object instance on the managed heap.

    More precisely:

    • A class is a reference type. A variable of that class (for example, Employee emp;) holds a reference. That reference itself is typically stored on the stack when it’s a local variable, or in another object/field if it’s a member.
    • The new operator allocates the actual Employee object on the managed heap. That object’s instance fields (which back the properties) live in that heap allocation.
    • Properties in C# are essentially methods (get/set accessors) plus, usually, a private backing field. The backing field that holds EmployeeId or EmployeeName is part of the object’s memory layout on the heap.

    So for an Employee class with properties EmployeeId and EmployeeName:

    • The local variable emp (the reference) is on the stack (or in another object if it’s a field).
    • The Employee instance is on the managed heap.
    • The data for EmployeeId and EmployeeName (their backing fields) is stored inside that Employee instance on the managed heap.

    Structs differ: a struct variable holds the value itself, so its fields (equivalent to property backing fields) are stored wherever the struct instance is stored (stack or inline in another object).


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.