none
why the value types are stored in stack and reference types are stored in heap RRS feed

  • Question

  • why the value types are stored in stack

    and reference types are stored in heap.

    why both are not stored in a same location.

    • Moved by Aspen VJ Friday, March 4, 2011 3:31 AM (From:Visual C# General)
    Thursday, March 3, 2011 6:58 AM

Answers

All replies

  • Hi

    U can store values types in heap and reference types in stack also

    This is called boxing and unboxing.

    For further details see this link

    http://msdn.microsoft.com/en-us/library/yz2be5wk(VS.80).aspx

     


    Regards TurbulentGeek "
    Mark as answer if it helped you..."
    ;
    Thursday, March 3, 2011 7:02 AM
  • You cannot unbox reference types.

    Value types are created on the stack only when they are not created as members of a reference type. Why? Because the implementers thought it was the more efficient way. When the method ends, you don't need to do anything to release value types on the stack except return the stack pointer to its initial position.

    Why are reference types in the heap? Because if they were on the stack, they could not survive after the method ends.

    Thursday, March 3, 2011 10:06 AM
  • You cannot unbox reference types.

    Value types are created on the stack only when they are not created as members of a reference type. Why? Because the implementers thought it was the more efficient way. When the method ends, you don't need to do anything to release value types on the stack except return the stack pointer to its initial position.

    Why are reference types in the heap? Because if they were on the stack, they could not survive after the method ends.


    hai louis

    If the reference type is declared in a method i.e local to the method.

    is it available out side of the method also.if available how can we access it.

     

     

    Thursday, March 3, 2011 10:47 AM
  • A value type needs a kind of value for its address on the stack. 

    As soon as the value for the reference is (almost) smaller or equal to the value it needs to set the reference, then it is not reasonable anymore to put it in the heap.

    16 bytes is taken for instance as the limit where a class is more reasonable to use than a structure (which is a kind of value type).


    Success
    Cor
    Thursday, March 3, 2011 11:00 AM
  • The local variables are not accessible from outside the method which declare them. Their value must be stored at class level to allow other methods to access them.
    Thursday, March 3, 2011 11:26 AM
  • Hi chintalapudi,

    This forum is dedicated to Visual C# technology. I'm moving this thread from Base "C# Forms" to "CLR" forum, since this issue is related to CLR. There are more CLR experts in that forum.

    Thank you for understanding and supporting.

     

    Best Regards,

    Vin Jin


    Vin Jin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 4, 2011 3:31 AM
  • Hi Vin Jin, 

    Who have moved this thread from C# general to Base C# forms, it is a typical good thread for the use for program languages like C# why to take a struct instead of class, which is often misunderstood. 

    Therefore a pity that you have moved it away from that C# program language forum.

    So very much related with programming and for sure not with windows forms and only reflected with the CLR.

    "Why" can here be seen technical but also abstract, and because that end users get it done for them, the abstract way is more important in my idea.

    :-( 

    Just to inform you.

    :-)


    Success
    Cor
    Friday, March 4, 2011 10:41 AM