当前位置:网站首页>Part C - value types and reference types

Part C - value types and reference types

2022-06-23 12:40:00 First wife ash yuanai

1. Value type and Reference type ( contrast )

Value type :

byte,short,int,long,float,double,decimal,char,bool and struct Collectively referred to as value types .

Reference type :

string and class Collectively referred to as reference types .

Concept : Value types store their values directly ( Represents actual data ), Reference types store references to their values ( Express Points to... Stored in the memory heap Pointers and references to data )

Base class : Value types are implicitly derived from System.ValueType, The reference type base class is Object

Storage : The value type is stored in the memory stack , Fast access , Stack memory is automatically released ;

            Data of reference type is stored in memory heap , The memory unit stores the address stored in the heap , Slow access , Heap memory is .Net Species GC( Garbage collection mechanism ) To automatically release

premium :

  • After value type variable declaration , Whether assigned or not , The compiler allocates memory for it .
  • Reference type when declaring a class , Only a small piece of memory is allocated in the stack to accommodate an address , At this time, there is no memory space allocated on the heap . When using new When creating an instance of a class , Allocate space on the heap , And save the address of the space on the heap to the small space allocated on the stack .
  • Instances of value types are usually allocated on the thread stack ( Static allocation ), But in some cases it can be stored in the heap .
  • Objects of reference type are always allocated in the process heap ( Dynamic allocation )
  • The amount of space allocated by the value type in the stack varies depending on the variable type ;

  • The reference type has the same space in the stack ;

 

 

原网站

版权声明
本文为[First wife ash yuanai]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231202149687.html