#Introduction to Generics:
Generic Type (Generic Type) is one of the most powerful features of .NET Framework2.0. The main idea of generics is to completely separate algorithms from data structures, so that an algorithm defined once can act on multiple data structures, thereby achieving highly reusable development. Generics allow you to define type-safe data structures without the need to use actual data types, which will significantly improve system performance and result in high-quality code (since data processing algorithms can be reused, there is no need to duplicate type-specific code).How generics work:
Generics can be used to define classes that are type safe and do not harm performance or work efficiency. On the surface, the syntax of C# generics is similar to that of C templates, but there are important differences in the way compilers implement and support them. Compared with C templates, C# generics can provide enhanced security, but are also somewhat limited in functionality. In some C compilers, the compiler won't even compile the template code until the template class is used via a specific type. When a type is indeed specified, the compiler inserts the code inline and adds every occurrence of a general type parameter. Places are replaced with the specified type. Furthermore, whenever a specific type is used, the compiler inserts code specific to that type, regardless of whether the type has been specified for the template class elsewhere in the application, which the C linker has to solve, but does not Doesn't always work, and this can also lead to code bloat, which increases load times and memory footprint. In .net Framewrok 2.0, generics have native support in the IL and the CLR itself. When compiling generic C# code, like any other type, first the compiler will compile it into IL, however, IL only contains parameters or placeholders of the actual specific type, and there are dedicated IL instructions to support generic operations. Generic code contains generic information in its metadata. The real generic instantiation work occurs in an "on-demand" manner during JIT compilation. When JIT compilation is performed, the JIT compiler replaces T in the generic IL code metadata with the specified type actual parameter to instantiate the generic type. This causes the JIT compiler to provide type-specific IL metadata definitions as if the generics were never involved. The JIT compiler can ensure parameter correctness, implement type safety checks, and even perform type-specific IntelliSense. When .net compiles generic IL code to native code, the resulting native code depends on the specified type. The JIT compiler tracks that IL code of a specific type has been generated. If the native value type is specified, the JIT compiler replaces the generic type parameter with the specified value type and compiles it into native code. If the JIT compiler compiles generic IL code with a value type that has been compiled to native code, only a reference to the IL code is returned. Because the JIT compiler will use the same value type-specific IL code on all subsequent occasions, there is no code bloat problem. If the native specification is a reference type, the JIT compiler replaces the generic parameter of the generic IL type with object and compiles it into native code. On any future requests for reference types rather than generic type parameters, the JIT compiler will simply reuse the actual code, with which instances are still allocated space at the size they left the managed heap, and there will be no casts .The above is the detailed content of What are .net generics?. For more information, please follow other related articles on the PHP Chinese website!