I. Value Type and reference type
1. Value Type:
Storage location: stored in the memory Stack
Including: simple type (INT, double, float, etc)
Structure Type (struct types)
Enumeration type (enmu types)
2. Reference Type:
Storage location: stored in the memory heap
Including: Class, interface, array, string
3. Value Type and reference type
Features |
Value Type |
Reference Type |
Storage location |
Stack |
Heap |
Default Value |
0 |
Null |
Parameters passed to the Method |
Copy Value |
Reference |
Variable Storage |
Actual value |
Reference address |
4. packing and unpacking
Packing: The value type is automatically converted to the reference type.
Example: int num = 200;
Object OBJ = num;
Binning: converts a reference type to a value type.
Example: object OBJ = 200;
Int num = (INT) OBJ;
Disadvantages of packing: it consumes more time than unpacking
Note: Do not pack or unpack the box at will in the program.
Ii. enumeration: belongs to the value type and is used to define a group of constants.
1. Definition restrictions: you cannot define your own methods, implement interfaces, or define attributes or indexes.
2. Statement:
Statement Syntax:
Access modifier Enum name
{
Enumeration member
}
Note: Members are separated by commas (,).
Example: Public Enum sex
{
Boy,
Girl
}
3. Usage:
Use Cases:
A) defines static constants and values are arranged in order
B) the members can be exhaustive.
Iii. structure (value type)
1. concept: it is a programmer-defined data type that is similar to a class and has different implementation methods and classes.
2. Comparison with classes
Class |
Structure |
Reference Type |
Value Type |
Initial value that can be assigned |
Initial values cannot be assigned to declared fields. |
The parameter-free constructor can be declared. |
No parameter-free constructor can be declared |
Must be instantiated |
New is not required for instantiation. |
Can be inherited except sealed class |
Cannot be inherited |
3. Declaration Syntax:
Struct name
{
Member
}
Note: The members are fields, attributes, methods, and parameter structures.