Reference type or value type ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Allwin Jeba
    New Member
    • Jan 2011
    • 7

    Reference type or value type ?

    This is a basic question which is confusing me..

    Here is the sample code..

    Code:
    class Program
        {
            static void Main(string[] args)
            {
                c obj = new c();
                obj.a = 11;
    
            }
        }
        class c
        {
            public int a = 10;
        }
    Now my question is we know integer is a value type and classes/objects are reference types..

    Then obj.a now a is a value type of a reference type ?

    since without boxing we can directly assign a value to obj.a = 11 which suggests that it is a value type.. but isn't "a" under the object obj which is reference type ?

    How it is stored internally ?
    Where is "a" stored ?
    where is "obj" stored?

    as far as I know a should be stored in a stack as it is value type and obj in heap as it is reference type.. but how they are associated ? can heap hold stack ? how does it work ?
  • Joseph Martell
    Recognized Expert New Member
    • Jan 2010
    • 198

    #2
    obj is of type "c" which is a reference type. That means that all of the object, including any private or public members, are part of its heap-allocated memory. In other words: a is on the heap because it is a part of obj, which is allocated on the heap.

    The .Net framework does not require you to explicitly box your value-type variables. The line:
    Code:
    obj.a = 11;
    performs boxing automatically without any effort from you.

    Some other resources that you might want to check out in reference to value and reference types:



    Comment

    • Allwin Jeba
      New Member
      • Jan 2011
      • 7

      #3
      are you sure ? because integer is a value type.. so isn't it supposed to be on stack ?

      Comment

      • Joseph Martell
        Recognized Expert New Member
        • Jan 2010
        • 198

        #4
        A good question. The second article that I linked to actually addresses that very question.

        Value-types variables are not always on the stack. They merely can be on the stack, depending on their scope and lifetime. Reference type variables, in contrast, must be on the heap.

        Consider this example:

        Code:
            class Program
            {
                static void Main(string[] args)
                {
                    Test();
                }
        
                static void Test()
                {
                    int myInt = 5;
                }
            }
        In this case, myInt is a local-scoped, value-type variable. When the Test() method is executed, the stack pointer is advanced far enough to allocate storage for myInt. When Test exits, the stack is "unwound" and the pointer is moved back. The storage allocated on the stack for myInt is now gone.

        The stack is deterministic and finite. Upon entering a scope, the stack pointer is advanced. Upon exiting a scope, the stack is unwound. Period. The stack is a giant last-in-first-out (LIFO) data structure.

        However, obj, in your original example, is not on the stack. It is a reference type and therefore must be on the heap. The lifetime of obj's member 'a' must match the lifetime of obj. 'a' cannot be on the stack because the lifetime of obj, and therefore 'a', is not predetermined or finite.

        Comment

        • Allwin Jeba
          New Member
          • Jan 2011
          • 7

          #5
          Thanks Joseph for clearing up.. and that link was a very good read..

          So to conclude it is :

          If the scope of the variable is local usually variables inside the method they will be allocated to stack. If the scope of the variable is class level then the variable will be allocated to heap and this is decided by the CLR..

          Thanks a lot.. :)

          Comment

          Working...