How does C# declare a variable to allocate memory space

Value type: int a, directly allocates memory space in the stack (stack) to store values.

Reference type: preson a=new preson(), divided into two steps

  1. Preson a, declares, allocates space for storing references on the stack, of course, at this time, a points to null (because no value has been assigned, the default address of the reference type is null)
  2. a=new preson(), in the managed heap (in c#), allocate the memory space of type preson, and store the address of the memory space in the storage space of a on the stack,

Therefore, what is stored in a is not the preson instance, but the address reference of the instance.

Guess you like

Origin blog.csdn.net/Ling_SevoL_Y/article/details/130200758