What is a generic type placeholder, or called type parameters. We know that in one method, the value of a variable can be used as a parameter, but in fact, the type of variable itself can also be used as a parameter. The generic allows us to specify this type of parameters when calling. In .NET, generics can give us two obvious benefits - type safety and decrease in packing, unpacking.
Type safety and packing, unbo
As a type of parameters, generics are easy to bring us type security. And before, in .Net1.1 we have to achieve type security can do this:
// Suppose you have a person collection public class person {private string _name; public string name {get {return _name;} set {_name = value;}}} // assumes that you have a PUBLIC Class Personcollection: IList {. .. private arraylist (); public person this [int index] {get {return (person) _PERSONS [index];}} public int address {_PERSONS.ADD (Item); return_persons. Count - 1; Person Item) {_Persons.Remove (item);} Object ilist.this [int index] {get {return_Persons [index];} set {_PERSONS [index] = (person) Value ;}}}} Int ilt.add (object item) {Return Add ((Person)} void ilist.remove (object item) {transove ((Person) iTem);} ...}
The above code mainly adopts the Explicit Interface Member Implementation technology, which can implement type security, but the problem is:
· Generate duplicate code. Suppose you have a Dog class collection, which is the same, but for type security, you have to copy a code, so that the program repeated code increases, when faced, it is more difficult to maintain.
Public class dogcollection: ilist {... private arraylist (); public dog this [int index] {get {return (DOG) _dogs [index];}} public int Add (DOG ITEM) {_dogs.add (Item); return_dogs.count - 1;} public void remove (Dog Item) {_dogs.remove (item);} Object ilist.this [int index] {get {return_dogs [index];} set {_dogs [ Index] = (DOG) Value;}} IList.Add (Object Item) {Return Add ((DOG) Item);} void ilist.remove (object item) {Remove ((DOG) iTem);} ... }
If in generics, you need to implement type security, you don't need to copy any code, you just need this:
List
PUBLIC CLASS INTCOLLECTION: ILIST {... private arraylist (); public int this [int index] {get {return (int) _ints [index];}} public int address {_ints.add (Item); return_ints.count - 1;} public void remove (IT ITEM) {_INTS.Remove (item);} Object ilist.this [int index] {get {return_ints [index];} set {_ints [ INDEX] = (int) value;}} int ilue;} {return add ((int) iTem);} void ilist.remove (object item) {transove ((int));} ... } Static void main (string [] args) {INTCOLLECTION INTS = new intcollection (); INTS.ADD (5); // Packing INT i = INTS [0]; // Unbox}
A small amount of packing, the unpacking of the unpacking is not large, but if the data volume of the collection is very large, the performance has a certain effect. The generic can avoid the type of packing and unpacking the value, you can confirm the compiled IL.
Static void main () {list
Generic implementation
· Wide method
Static Void Swap
· Pan type, structure
Public class point
Generic WHERE
The generic WHERE can limit the type parameters. There are several ways.
· WHERE T: STRUCT Limit Type Parameters T must inherit from System.ValeType.
· WHERE T: Class Limit Type Parameter T must be a reference type, that is, cannot inherit from System.ValeType.
· WHERE T: New () Restriction Type Parameters T must have a default constructor
· WHERE T: NameOfClass Limit Type Parameters T must inherit from a class or implement an interface.
These qualifications can be used in combination, such as: public class point
Generic mechanism
·mechanism:
The C # generic code uses special placeholders to represent generic types with special placeholders when compiled into IL code and no data, and supports generic operation with proprietary IL instructions. The real generic instantiation works in the "on-demand" mode, occurs in JIT compile.
· Compilation mechanism:
1. When the first round is compiled, the compiler only generates "generic version" IL code and metadata for the Stack
2. When JIT compiles, when the JIT compiler encounters STACK
Some problems of generics
· Do not support operator overload. I only know so much.
Significance
What is the meaning of generic? Types of security and decrease in packages, unpacking is not generic, but two benefits brought by generics (perhaps in .NET generics, this is the most obvious benefit). The meaning of generic is - use the type as a parameter, which implements a good horizontal connection between the code. We know that inherits the inheritance provides a longitudinal connection from the top, but the generic landscape Contact (From a certain extent, it and AOP have in thought). In the PersonCollection example, we know the same type of the add () method and the transove () method, but we can't tell our program, generic provides a mechanism to let the program know these. Although the truth is simple, such a mechanism may bring some profound changes to our procedures.