What is a generic

xiaoxiao2021-04-10  309

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 Persons = new list (); persons.add (new person ()); pers message = persons [0]; list dogs = new list (); dogs.add ( New Dog ()); DOG DOG = DOGS [0]; • The object of the value is also required, and additional packing is required. In fact, for the traditional collection, as long as the content contained in it involves the value type, it is inevitable that it is necessary to pack, unpack. Please see the example below.

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 INTS = new list (); INTS.ADD (5); // Do not use the box I = INTS [0]; // No unpack}

Generic implementation

· Wide method

Static Void Swap (Ref T a, Ref T B) {Console.Writeline ("You Sent The Swap () Method A {0}", TypeOf (T)); T Temp; Temp = A; A = B ; B = TEMP;

· Pan type, structure

Public class point {private t _x; private t _y; public tx {get {_x = value;}} public type {get}} public type {get}}}} set {_y = value;}} public OVERRIDE STRING TOSTRING () {Return String.Format ("[{0}, {1}]", _x, _y);}}

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 where t: class, iComparable, new ()

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 (Stack Algorithm) type ----- does not perform the instantiation of generic types, T is Only act as a placeholder in the middle

2. When JIT compiles, when the JIT compiler encounters STACK , the "generic version" IL code and the T ---- of the T ---- of the metadata will be used in the INT. The CLR generates the same copy of the generic type for all types of parameters to "reference type"; but if the type parameter is "value type", the CLR will generate a separate code for each different "value type".

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.

转载请注明原文地址:https://www.9cbs.com/read-133382.html

New Post(0)