C # Generic

Source: Internet
Author: User

C # generic classes and structures
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.
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
Class G: C <u, v> {}// Invalid

Generic Type members
Class C <v> {
Public v F1; // declare a field
Public d <v> F2; // as a parameter of other generic types
Public C (v x ){
This. F1 = X;
}
}
Generic Type members can use the type parameters in the generic type declaration. If the type parameter has no constraints, only public members inherited from system. object can be used for this type.

Generic Interface
Interface ilist <t> {
T [] getelements ();
}
Interface idictionary <K, V> {
Void add (K key, V value );
}
// The type parameter of the generic interface is either instantiated,
// Either comes from the type parameter of the implementation class declaration
Class list <t>: ilist <t>, idictionary <int, T> {
Public T [] getelements () {return NULL ;}
Public void add (INT index, T value ){}
}
Generic Delegation
Delegate bool predicate <t> (T value );
Class X {
Static bool F (int I ){...}
Static bool g (string s ){...}
Static void main (){
Predicate <string> P2 = g;
Predicate <int> P1 = new predicate <int> (f );
}
}
Generic delegation supports applying parameter types to delegate return values and parameters. These parameter types can also be subject to legal constraints.
Introduction to generic methods
• C # The generic mechanism only supports "Adding type parameters to method declarations"-that is, generic methods
• C # The generic mechanism does not support the inclusion of class parameters in the declaration of other members except the method (including attributes, events, indexers, constructors, and Destructors, however, these members can be included in the generic type and use the generic type parameters.
• Generic methods can be included in both generic and non-generic types.
Declaration and call of generic methods
// A non-generic class is a specific class, which does not need to be instantiated by the generic type
Public class finder {
// But it is a generic method. Please refer to the declaration of the generic method. parameters must be generic.
Public static int find <t> (T [] items, t item ){
For (INT I = 0; I <items. length; I ++ ){
If (items [I]. Equals (item) {return I ;}
}
Return-1;
}
}
// Call a generic method <int> not after the finder, but after the find.
Int I = finder. Find <int> (New int [] {1, 3, 4, 5, 6, 8, 9}, 6 );

Overload of generic methods
Class myclass {
Void F1 <t> (T [] A, int I ); // Cannot constitute an overload method
Void F1 <u> (U [] A, int I );
Void F2 <t> (int x ); // Can constitute an overload method
Void F2 (int x );
// The two statements are the same. The WHERE clause and T must inherit a. The generic parameter must inherit.
Void F3 <t> (t) where T:; // Cannot constitute an overload method
Void F3 <t> (t) where T: B;
}
Override of generic methods
Abstract class base
{
Public abstract t f <t, u> (t, u) where u: T;
Public abstract T g <t> (t) where T: icomparable;
}
Class derived: Base {
// Valid rewrite. The constraint is inherited by default. You only need to write the method signature.
Public override x F <X, Y> (x, y ){}
// Invalid rewrite. It is unnecessary to specify any constraints.
// When rewriting, you cannot write or add new constraints. You can only inherit the constraints of the parent class.
Public override T g <t> (t) where T: icomparable {}
}
Introduction to generic Constraints
• C # any assumptions about "type parameters for all generic types or generic methods" must be maintained based on explicit constraints.
C # required type security.
• Explicit Constraints are expressed by the WHERE clause. You can specify "base class constraints" and "interface constraints ", the constructor constraint and value type/reference type constraint are four types of constraints.
• Explicit Constraints are not mandatory. If explicit constraints are not specified, generic parameters can only access public methods in the system. Object type.
Base Constraint
Class A {public void F1 (){...} }
Class B {public void F2 (){...} }
Class C <S, T>
Where s: A // s inherited from
Where T: B // t inherited from B
{
// You can call F1 on a variable of type S,
// You can call F2 on a variable of the T type.
....
}
Interface Constraints
Interface iprintable {void print ();}
Interface icomparable <t> {int compareto (t v );}
Interface ikeyprovider <t> {T getkey ();}
Class dictionary <K, V>
Where K: icomparable <k>
Where V: iprintable, ikeyprovider <k>
{
// You can call compareto on a variable of K type,
// Print and getkey can be called on a variable of the V type.
....
}
Constructor Constraints
Class A {public (){}}
Class B {public B (int I ){}}
Class C <t>
Where T: New ()
{
// T = new T () can be used in it ();
....
}
C <A> C = new C <A> (); // yes. A has no parameter constructor.
C <B> C = new C <B> (); // error. B has no parameter constructor.
Value Type/reference type constraints
Public struct {... }
Public Class B {... }
Class C <t>
Where T: struct
{
// T is a value type.
...
}
C <A> C = new C <A> (); // yes. A is a value type.
C <B> C = new C <B> (); // error. B is a reference type.

Summary
• The generic capability of C # is supported by CLR during runtime. It is different from the static template supported by C ++ during compilation, it is also different from the simple generic type supported by Java's use of the "batch wipe" method at the compiler level.
• C # supports four generic types, including class, structure, interface, and delegate, and method members.
• The generic type of C # adopts the "base class, interface, constructor, value type/reference type" constraint to implement the "Explicit constraint" on type parameters ", it does not support signature-based implicit constraints like the C ++ template.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.