Are these objects's references on the Stack or on the Heap?

前端 未结 8 1992
猫巷女王i
猫巷女王i 2020-11-27 14:51

I would really appreciate if someone could tell me whether I understand it well:

class X
{
   A a1=new A(); // reference on the stack, object value on the he         


        
相关标签:
8条回答
  • 2020-11-27 15:22

    I think you might have have a slight misunderstanding...

    Generally speaking, reference types go on the heap, and value types / locals I believe (may be wrong) go on the stack. However, your A1.VarA and A2.VarA examples are referring to a field of a reference type - which is stored together with the object on the heap...

    0 讨论(0)
  • 2020-11-27 15:30

    Remember reading in C# in Depth :- Only local variables (the one declared inside method) and method parameter live in stack.Instance variable like varA in above case reside on heap.

    0 讨论(0)
提交回复
热议问题