C # general programming Basics

Source: Internet
Author: User

Through a deeper understanding of the project and the use of the design model, it is inevitable that it will not be transported to the out-of-the-box and packing operations. The normal out-of-the-box and packing operations have a certain impact on the system performance. To solve this problem, one of the solutions is to use generics. The following is a brief introduction and use of C #2.0 generics, which is easy to use in projects.
I. C # Generic demonstration

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];}
}

introduction to C # generics
stack X = new stack ();
X. push (17);
the so-called generic type means that multiple data types can be operated on the same Code using parameterized types. Generic programming is a programming paradigm that abstracts data types using "parameterized types" to achieve more flexible reuse. C # generics give the code stronger type security, better reuse, higher efficiency, and clearer constraints.
III. Introduction to C # generics
C # generics are supported by CLR at runtime, which is different from the C ++ compiler template mechanism, and java compilation, which 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 the 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.
IV. C # Generic compilation mechanism
during a round of compilation, the compiler only generates the Il code and metadata of the "generic version" for the stack type. It does not instantiate the generic type. t serves as the placeholder JIT during compilation, when the JIT compiler encounters a stack for the first time, it replaces the "generic version" il code with the 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 ", CLR will generate an independent code for it.
5. Characteristics of C # generics
If 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.
the C # Generic Type carries rich metadata. Therefore, the C # generic type can be applied to powerful reflection technologies.
the C # generic model adopts the "base class, interface, constructor, value type/reference type" constraint to implement "Explicit constraint" on type parameters ", this improves type security while also losing the high flexibility of the C ++ template based on the "signature" implicit constraint.
VI. 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
Class G: C <u, v >{} // invalid

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.
VII. 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.
VIII. Generic Interfaces

Interface ilist <t> {
T [] getelements ();
}
Interface idictionary <K, V> {
Void add (K key, V value );
}

// The type parameter of the generic interface either has been 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 ){}
}

9. 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 );
}
}

10. Generic Method
You can apply 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 "containing type parameters in method declaration"-that is, generic methods
C # The generic mechanism does not support the inclusion of type parameters in declarations of other members except the method (including attributes, events, indexers, constructors, and Destructors, however, these members can be included in the generic type, and the generic method of the generic type parameter can be included in the generic type, it can also be included in non-generic types.
11. Declaration and call of generic methods

Public class finder {
// Declaration of generic methods
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 I = finder. Find <int> (New int [] {1, 3, 4, 5, 6, 8, 9}, 6 );

12. overload of generic methods

Class myclass {
Void F1 <t> (T [] A, int I); // The overload method cannot be constructed.
Void F1 <u> (U [] A, int I );
Void F2 <t> (int x); // overload method
Void F2 (int x );
Void F3 <t> (t) where T: A; // The overload method cannot be constructed.
Void F3 <t> (t) where T: B;
}

13. 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.
Public override x F <X, Y> (x, y ){}

// Invalid rewrite. It is unnecessary to specify any constraints.
Public override T g <t> (t) where T: icomparable {}
}

14. Introduction to generic Constraints
C # any assumption of "all generic type or generic method type parameters" must be based on "Explicit Constraints ", to maintain the type security required by C. "Explicit constraint" is expressed by the WHERE clause. You can specify "base class constraint", "interface constraint", "constructor constraint ", there are four constraints in "value type/reference type constraint. "Explicit constraint" is not mandatory. If "Explicit constraint" is not specified, generic parameters can only access public methods in the system. Object type.
15. Base Constraints

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.
}

16. 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.
}

17. constructor Constraints

Class {
Class B {

Class C <t>

{
Public (){}}
Public B (int I ){}}
}

C <B> C = new C <B> (); // error. B has no parameter constructor.
Where T: New ()

// T = new T () can be used in it ();
.

C <A> C = new C <A> (); // yes. A has no parameter constructor.

18. 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.

 

Source: http://www.cnblogs.com/Charles2008/archive/2008/06/11/1217294.html

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.