generic
1. What is generic?
type security classes can be defined through generics, without compromising type security, performance, or productivity
2. instantiate a generic
1. Declare and instantiate any type.
2. A specific type must be used to replace the general type T
3. Example:
//
public class Stack
{
object [] m_items;
Public void push (Object item)
{...}
Public object POP ()
{...}
}< SPAN class = "apple-converted-space">
Stack stack = new stack ();
stack. push (1);
int number = (INT) stack. pop ();
//
public class Stack
{
T [] m_items;
Public void push (T item)
{...}
Public t POP ()
{...}
}< SPAN class = "apple-converted-space">
Stack stack = new stack ();
stack. push (1);
int number = (INT) stack. pop ();
Iii. Benefits of generics
1. One-time development, testing and deploymentCode, Reuse it by any type
2. compiler support and type security
3. The value type is not forcibly packed or unpacked, or the reference type is forcibly converted downward, so the performance is significantly improved.
Note: The value type can be increased by about 200%, and the reference type is about 100%.
4. Multiple generic types
1. Multiple generic types can be defined for a single type
5: wildcard alias
1. Use Using in the file header to get an alias of a specific type, the alias applies to the entire file
2. Example
using list = sort list ;
class listclient
{
static void main (string [] ARGs)
{
List list = new list ();
list. addhead (123, "AAA ");
}< SPAN class = "apple-converted-space">
}
V. Generic Constraints
(1) derived Constraints
For example:
Public class extends list <K, T> where K: icomparable
{
T find (K key)
{
If (Str. Key. compareto (key) = 0) // only this interface can be compared.
}
}
Note:
1. All derivation constraints must be placed after the actual derivation list of the class.
For example, public class Sequence List <K, T>: ienumerable <t> where K: icomparable <k>
{...}
2. Multiple Interfaces can be restricted on a generic parameter (separated by commas)
Public class merge list <K, T> where K: icomparable <k>, iconvertible
3. A maximum of one base class can be used in a constraint.
4. The constraint base class cannot be a SEAL class or a static class.
5. You cannot restrict system. Delegate or system. array to the base class.
6. A base class and one or more interfaces can be constrained at the same time, but the base class must first appear in the derived constraint list.
7. C # allows you to specify another generic parameter as a constraint
Public class myclass <t, u> where T: u
{...}
8. You can customize basic classes or interfaces for generic constraints.
9. custom interfaces or base classes must have consistent visibility with generics
(2) constructor Constraints
For example:
Class node <K, T> where T: New ()
{
}
Note:
1. constructor constraints and derived constraints can be combined on the premise that constructor constraints appear at the end of the Constraints List.
(3) Constraints on reference/value types
1. You can use struct constraints to constrain generic parameters to value types (such as int, bool, and enum) or any custom structure.
2. You can also use the class constraint to restrict generic parameters to reference types.
3. You cannot use the reference/value type constraints with the base class constraints because the base class constraints involve classes.
4. Structure and default constructor constraints cannot be used, because the default constructor constraints also involve class
5. Although you can use the class and default constructor constraints, this method has no value.
6. You can combine the reference/value type constraints with the interface constraints, provided that the reference/value type constraints appear at the beginning of the Constraints List.
Vi. Generic and forced type conversion
1. C # The Compiler only allows implicit conversion of generic parameters to objects or types specified by constraints.
For example:
Interface is {...}
Class baseclass {...}
Class myclass <t> where T: baseclass, is
{
Void somemethod (T)
{
Is obj1 = T;
Baseclass obj2 = T;
Object obj3 = T;
}
}
2. the compiler allows you to forcibly convert generic parameter display to any other excuse, but cannot convert it to class
Interface is {...}
Class someclass {...}
Class myclass <t> // no constraints
{
Void somemethod (T)
{
Is obj1 = (is) T; // Yes
Someclass obj2 = (someclass) T // No
}
}
3. You can use temporary object variables to forcibly convert generic parameters to any other type.
Class someclass {...}
Class myclass <t>
{
Void somemethod (T)
{
Object temp = T;
Someclass OBJ = (someclass) temp; // Yes
}
}
Note: This only tells you that writing like this is acceptable, but do you want to write like this? Do not write it like this, because if t does not inherit someclass, compilation is correct, but an error will occur during running.
4. To solve the above forced conversion problem, you can use the is and as operators for determination.
Public class myclass <t>
{
Public void somemethod <t>
{
If (T is int ){...}
If (T is invalid list <int, string> ){...}
// If the generic parameter type is the queried type, the is Operator returns true.
String STR = T as string;
// If this write type is compatible, as will perform forced type conversion; otherwise, null will be returned.
If (STR! = NULL ){...}
Counter list <int, string> List = T as counter list <int, string>;
If (list! = NULL ){...}
}
}
7. Inheritance and generics
1. When derived from a generic base class, you can provide type arguments instead of base class generic parameters.
Public class baseclass <t> {...}
Public class subclass: baseclass <int>
2. If the subclass is generic, rather than the actual type parameter, you can use the subclass generic parameter as the specified type of the generic base class.
Public class baseclass <TT> {...}
Public class subclass <t>: baseclass <t> {...}
3. When using a subclass generic parameter, any constraints specified at the base class level must be repeated at the subclass level.
4. The base class can define its signature using the generic parameter's virtual method. When rewriting them, The subclass must provide the corresponding type in the method signature.
For example:
Public class baseclass <t>
{
Public Virtual t somemethod ()
{...}
}
Public class subclass: baseclass <int>
{
Public override int somemethod ()
{...}
}
5. If this subclass is generic, it can also use its own generic parameters during rewriting.
Public class subclass <t>: baseclass <t>
{
Public override t somemethod ()
{...}
}
6. You can define generic interfaces, generic abstract classes, and even generic abstract methods.
7. You cannot use operators like ++ or ++ = for generic parameters.
Public class calculator <t>
{
Public t add (T arg1, t arg2)
{
Return arg1 + arg2; // Error
}
}
However, we can implement a function through the generic abstract class and interface, because we have already explicitly passed a parameter to implement the generic abstract class and interface, so that we can execute operations such as +.
8. Generic Method
1. methods can define generic parameters specific to their execution scope
Public class myclass <t>
{
Public void mymethod <x> (X)
{...}
}
2. Even if each containing class does not use generics at all, you can define method-specific generic parameters.
Public class myclass
{
Public void mymethod <t> (T)
{...}
}
Note: This function only applies to methods and attributes. The indexer can only use generic parameters defined in the scope of action of the class.
3. Call generic methods
Myclass OBJ = new myclass ();
OBJ. mymethod <int> (3 );
You can also do this:
Myclass OBJ = new myclass ();
OBJ. mymethod (3); // This function is called generic reasoning.
4. generic methods can also have their own generic parameter Constraints
Pubic class myclass
{
Public void somemethod <t> (t) where T: icomparable <t>
{...}
}
5. Constraints that appear repeatedly at the parent level for subclass method implementation
Public class baseclass
{
Public Virtual void somemethod <t> (t) where T: New ()
{...}
}
Pubic class subclass: baseclass
{
Public override void somemethod <t> (t) // no more constraints
{...}
}
6. Static Methods
Static methods can define specific generic parameters and constraints
Public class myclass <t>
{
Public static t somemethod <x> (t, x)
{...}
}
Int number = myclass <int>. somemethod <string> (3, "AAA ");
Or: int mumber = myclass <int>. somemethod (3, "AAA ");
9. Generic Delegation
1. Delegation defined in a class can use the generic parameters of the class.
2. The delegate can also define its own generic parameters.
Http://topic.csdn.net/u/20070903/17/0B06562A-19B9-4BAE-85C3-0FB1AE1D6037.html