I started to think this way about programming, but it is better for us now. For example, when the language develops, it becomes easier to understand and program. tools also develop, making programming easier, such as smart prompts.
However, this understanding seems a bit one-sided. If you don't understand the development history of a technology, it will be hard to really understand it. C # Generic programming is only available in 2.0, but your understanding is not enough. I would like to summarize it.
C # generics and mechanisms
C # Generic demonstration
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { Stack<int> test = new Stack<int>(); test.Push(10); Console.WriteLine(test.GetResult()); Console.Read(); } } class Stack<T> { private T[] store; private int size; public Stack() { store = new T[10]; size = 0; } public void Push(T x) { store[size++] = x; } public T Pop() { return store[--size]; } public string GetResult() { return store[--size].ToString(); } }}
C # Introduction to generics
The so-called generic type refers to the operation of multiple data types on the same code by using parameterized types. Generic programming is a programming paradigm that abstracts types by using parametric types to achieve more flexible reuse.
C # generics give the code stronger type security, better reuse, higher efficiency, and clearer Constraints
C # Introduction to generic mechanisms
C # The generic capability is supported by CLR at runtime. It is different from the C ++ compile-time template mechanism, and from the Java compile-time "batch erase ". This enables seamless interoperability between languages that support CLR.
C # When the generic code is compiled as the IL code and metadata, special Placeholders are used to represent the generic type, and proprietary IL commands are used to support generic operations. The real generic instantiation work is in the "on-demand" mode, which occurs during JIT compilation.
C # Generic compilation Mechanism
During the first round of compilation, the compiler only generates the "generic version" IL code and metadata for the Stack <T> type-it does not instantiate the generic type, and T acts as a placeholder in the middle.
During JIT compilation, when the JIT compiler encounters a Stack <int> for the first time, it replaces the "generic version" IL code with T -- in the metadata to instantiate the generic type.
CLR generates the same code for all generic types whose type parameters are "reference type". However, if the type parameter is "Value Type", for each different "Value Type ", CLR will generate a separate code for it
C # characteristics of generics
If the parameters of the instantiated generic type are the same, the JIT compiler will reuse this type, therefore, the dynamic generic capability of C # avoids code expansion problems caused by C ++ static templates.
• C # generic types carry rich metadata, So C # generic types can be applied to powerful reflection technologies.
• The generic type of C # adopts the "base class, interface, constructor, value type/reference type" constraint to implement the "Explicit constraint" on type parameters ", this improves the type security, and also loses the high flexibility of the C ++ template based on the "signature" implicit constraint.
Generic Type
C # generic classes and structures
Class C <U, V >{}// valid class D: C <string, int >{}// valid class E <U, V >:c <U, V >{}// valid class F <U, V >:c <string, int >{}// valid // illegal // class G: C <U, v> {};
C # besides declaring generic types (including classes and structures), you can also include the declaration of generic types in the base class. However, if a base class is a generic class, its type parameters are either instantiated or derived from the type parameters declared by the subclass (also generic type.