Fields of class, are they stored in the stack or heap?

Posted by Mirek on Stack Overflow See other posts from Stack Overflow or by Mirek
Published on 2010-04-02T06:32:41Z Indexed on 2010/04/02 6:43 UTC
Read the original article Hit count: 415

Filed under:
|
|
|

I saw a question yesterday which raised (for me) another question. Please look at the following code:

public class Class1
{
   int A; //as I uderstand, int is value type and therefore lives in the stack
}

class Class2
{
    Run()
   {
       Class1 instance1 = new Class1();
       instance1.A = 10;  //it points to value type, but isnt this reference (on heap)?
   }
}

Or while creating the instance of Class1, its field types are created on the heap as well? But then I do not understand when it would really be on the stack as almost always you need to create an instance of object in order to use it fields.

© Stack Overflow or respective owner

Related posts about c#

Related posts about memory-management