. NET basic step by step [object-oriented stack, stack, reference type, value type],. net Object-Oriented
Stack, stack, reference type, Value Type
Memory is divided into heap and stack (PS: there is also a static storage area [memory is divided into these three]), value-type data is stored in the stack, data of the reference type is stored in the heap.
Stack and stack:
Stack and stack differences:
- Stack is the memory space allocated during compilation. Therefore, your code must have a clear definition of the stack size. Local value type variables and value type parameters are all in the stack memory.
- Heap is the memory space dynamically allocated during the program running. You can determine the size of heap memory to be allocated based on the program running status.
Reference Type and Value Type
- 1. When a value type variable is assigned to another value type variable, the included values are copied. The value assignment of the reference type variable only copies the reference to the object, instead of copying the object itself.
- 2. The value type cannot be derived from a new type: All value types are implicitly derived from System. ValueType. But what is the same as the reference type is that the structure can also implement interfaces.
- 3. The value type cannot contain null values:
- 4. Each value type has an implicit default constructor to initialize the default value of this type.
- 5.
Extension:
Packing: convert value type to reference type
Binning: Convert the reference type to the value type
Instance:
. NETWhich are the value types in? What are the reference types?
Case study:
Class Program {static void Main (string [] args) {Person yaohongbo = new Person () {Name = "Yao Hongbo", Age = 29}; Person lilei = new Person () {Name = "Li Lei", Age = 22}; int age = yaohongbo. age; yaohongbo. age = 27; Person zhangsan = lilei; lilei. name = "Li Lei"; Console. writeLine ("yaohongbo age:" + yaohongbo. age); Console. writeLine ("age:" + age); Console. writeLine ("name of lilei:" + lilei. name); Console. writeLine ("name of zhangsan:" + zhangsan. name); Console. readKey () ;}} class Person {public string Name {get; set ;}// reference type public int Age {get; set ;}// Value Type}
Running result:
Code Description: yaohongbo. age is 'int' type and 'value' type, so the code int age = yaohongbo. age, which is to convert yaohongbo. the value stored in the Age variable is copied, and the variable age is used to store the copy. age does not have any relationship, so we can see the output above.
Look at the code again:
Person zhangsan = lilei; lilei. Name = "Li Lei ";
Person is a user-defined class and a reference type. The reference type is referenced. The data pointed to by the variable zhangsan and the variable lilei are the same memory address, that is, the same object, instead of copying the data pointed to by the variable lilei to the variable zhangsan as the value type. Therefore, no matter the attribute value of zhangsan Korean lilei is changed, the other will change.
This article is here, and finally a small advertisement: QQ group: . NET Step by stepGroup Number:590170361 (Add group remarks: what you see in the blog Park)