Data types can be stored in two categories: value types and reference types.
1 Value types
Value types originate from the System.ValueType family, and each object of value type has a separate memory area for saving its own values.
The memory region where the value type data resides is the stack (stack), which, as long as it is modified in the code, holds the value in its region, which mainly includes the base data type and enumeration type.
For value types, different variables allocate different storage spaces, and the value of the variable is stored in the storage space, and the value of the variable is passed by the assignment operation, and changing the value of one variable does not affect the value of the other variable.
2. Reference types
Reference types originate from the System.Object family, and in C # Reference types mainly include arrays, classes, interfaces, and so on.
For reference types, the assignment is to pass a reference to the original object to another reference, which, when an array reference is assigned to another array reference, points to the same array, that is, to the same block
Storage space.
Fine-score types and reference types
Value type: 1. Basic data Type 2. Enumeration Type 3. Structure type
Reference type: 1. Class 2. Interface 3. Array
Structure
1. Definition of structure
Access modifier struct struct name
{
Structural body
}
The definition of a structure has the following characteristics:
1. There can be fields in the structure, or there can be methods
2. When defining, the fields in the structure cannot be assigned an initial value
Use of structures
1. You can not use new
2. Once you declare an object of a struct, you must assign an initial value to the members of the struct.
Packing and unpacking
1 int 123 ; 2 object o = i; // Boxing: Value type conversion to reference type 3 int j= (int) o; // unpacking: Converting a reference type to a value type
Different types of parameter passing
1. How do I differentiate between a value pass or a reference pass?
The basis for judging is there is no ref.
2. When passing a value type parameter using a value method (without ref decoration), the modification of the parameter in the method is not persisted.
When a reference type parameter is passed using a value method (without a ref decoration), the modification of the parameter in the method is preserved
When you pass a value type or reference type parameter by using a reference method (with ref decoration), the modification of the parameter in the method is preserved
In-depth C # data types