[CSharp] C #2.0 sharp experience series (1): Generic programming-Study Notes

Source: Internet
Author: User

Question: Which of the following statements is incorrect?
1 class C <U, V> {}
2 class D: C <string, int> {}
3 class E <U, V>: C <U, V> {}
4 class F <U, V >:c <string, int> {}
5 class G: C <U, V> {}

Explanation
1. generic definition
2 D is a common class, C is instantiated generic class IMG src = "http://www.cnblogs.com/blog/smile19.gif">
3 E is a sub-generic type, C is a base generic type, C uses the sub-generic parameter policimg src = "http://www.cnblogs.com/blog/smile19.gif">
4 F is a generic Objective C already has an instance named javasimg src = "http://www.cnblogs.com/blog/smile19.gif">
5G is a class, and C is a generic class. Unzip IMG src = "C: \ Program Files \ BlogJet \ Data \ Smiles \ smilew.gif">

Note
You can include a declaration of the generic type in the base class. However, note: if the base class is a generic class, its type parameters are either instantiated or derived from sub-classes? /FONT>

--------

Generic Type? FONT>

Class C <V> {
Public V f1; // declare f1
Public D <V> f2; // reference other generic types to define Member f2
Public C (V x) {// Method
This. f1 = x;
}
}

---------

Generic Interface

Define a method to import a generic type. Define BR> interface IList <T> {
T {} MethodA ();
}
Interface IDictionary <K, V> {
Void MethodB (K k, V v );
}

Class List <T>: IList <T>, IDictionary <int, T>
{
Public T [] MethodA (){}
Public void MethodB (int I, T t ){}
}

----------

Generic Method

Public class Finder {
Public static int Find <T> (T [] items, T item)
{
For (int I; I <items. Length; I ++ ){
If (items [I]. Equals (item) {return I ;}
}
}
}

----------

What is the weight of the generic method? FONT>

Class c {
Void F <T> (int x );
Void F (int x );
Public abstract t f <T, U> (T t, U u) where U: T;
}

Rewrite
Class cc: c {
Public override x f <X, Y> (X x X, Y y ){}
}

-----------------

Base Constraint

Class A {public void F1 (){...}}
Class B {public void F2 (){...}}

Class C <S, T>
Where S:
Where T: B
{
// You can call the F1 Method on type S.
// The F2 method can be called on type B
...
}

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.