in principle:
1. Similar to C++ templates, the essence of C#’s generics is the type of type.
It defines a type, and each instantiated object of it is a specific type, so it is called a type of type
2. It is divided into two situations when instantiated:
Reference types and value types
Because the reference type is essentially a pointer, which is a memory address, the number of bytes used by the pointer variable is the same on a machine with a certain bit length. For example, a 32-bit machine uses 4 bytes.
The essence of generics is as a type of defined type. After compilation, there will be a binary code describing the characteristics of this type definition, which is stored in memory.
Here we first describe the type definition and memory allocation of instantiated objects after compiling the source code. After the source code is compiled, a program file (such as an exe file) must be programmed and loaded into the memory space during execution (modern OS uses mapping. , logically occupying memory, physically using paging method, using which part of the data to transfer that part of the data into the physical memory);
When defining a class, the description of the class after compilation (information about which data members, which member functions, respective permissions, etc.) forms a part of the exe file, which is loaded into the memory after running. This part of the binary data is set at the address of the memory. is 0x0001;
When instantiating a class object, it depends on the language. In C++, the class instantiation object allocates memory on the stack; in C#, the class instantiation object allocates memory on the heap; this part of the memory space is in the program's memory. space (such as Windows 32-bit system, the program memory space is 4G). In the remaining memory other than the memory mapped by the exe, when the instantiated object exceeds the lifetime or is released from the heap, the memory space is returned to the process.
Similarly, the generic encoded binary data is contained in the exe file and loaded into memory.
When a generic is instantiated, that is, when a specific type is compiled (note that the process of instantiating a generic in C# is performed during compilation, that is to say, the instantiated generic used in the code, For each specific type, binary code is generated during compilation, and the binary code of the generic itself is written into the exe file). How much space does the binary code of each instantiated type need to occupy in the exe file?
In C#, we need to distinguish between reference types and value types. Suppose a generic instantiates two reference types and two value types in the source code of the program.
Then for reference types:
One uses a 4-byte pointer, and two uses two pointers. The binary data points to the type used (such as myClass in vector<myClass>. myClass is a defined class type. After compilation, the binary data is stored in exe file) (the relative address is converted into a memory address after being loaded into memory). This is because the instantiated object of the reference type is allocated on the heap and executed at runtime. At this time, from the pointer The address obtains the binary data described by the myClass type, calculates the memory space occupied by an instantiated object, and allocates it on the heap.
For value types:
Value types include structures and predefined data types. When instantiating generics with such types (such as vector<int>, vector<double>), actual binary data of the two classes will be generated to represent them. Class description, so that when instantiating their objects in the program, it is fast (of course, it can also be calculated and encoded at runtime like a reference), but the value type is allocated on the stack, and the flexibility is lost in order to obtain Efficiency, so execute like this)