C # Learning diary 30 --- generic classes, generic methods, generic delegation

Source: Internet
Author: User

C # Learning diary 30 --- generic classes, generic methods, generic delegation

Generics allow you to delay writing the data type specification of the programming element in a class or method until it is actually used in a program. In other words, when declaring a class or method, because you do not know what type of parameters you want to input, "dig a hole (" <T> ")" in the input type ")", when using it, we can fill in it with specific data types.

Generic Type:

Based on the previous knowledge, we define a class:

Class Data

{

Public int n_data;

}

At this time, the Data Type of n_data has been determined to be int type. Therefore, when assigning values to n_data, it can only be int type. If it is changed to the following generic class:

Class Data

{

Public T n_data;

}

At this time, the Data Type of n_data is not determined. Therefore, when assigning values to it, you need to specify T, that is, the type of n_data, that is, fill in,

Data Data = new Data (); // Specify T as int

Data Data = new Data (); // Specify T as string

Of course, T cannot be specified as an array in the above example. If the n_data type is to be set to an array, the following example can meet the following requirements:

Using System; using System. Collections. Generic; using System. Linq; using System. Text; namespace Example {class Data
 
  
// Generic class {public T [] n_data; // The generic variable public Data (int size) // constructor, when new, call the constructor to open up space {n_data = new T [size];} // enter public void setdata (int index, T value) {n_data [index] = value ;} // output public T getdata (int x) {return n_data [x] ;}} class Program {static void Main (string [] args) {Data
  
   
Data = new Data
   
    
(5); data. n_data [2] = 2; Console. WriteLine (data. n_data [2]) ;}}
   
  
 


Result: 2

Generic method:

In this example, we use the swap switching method. In C ++, the swap function is written as follows:

# Include
 
  
Using namespace std; template
  
   
Void swap1 (T & a, T & B) // It can also be considered as a generic {T temp; temp = a; a = B; B = temp;} int main () {int a = 0, B = 1; swap1 (a, B); cout <
   

Result: 1 0. If a and B are character types and above, they are also applicable. C # swap method:

Using System; using System. Collections. Generic; using System. Linq; using System. Text; namespace Example {class data {// swap method, ref is the public static void swap passed by address
    
     
(Ref T a, ref T B) {T temp; temp = a; a = B; B = temp;} static void Main (string [] args) {string a = HC; string B = 666; swap (ref a, ref B); Console. writeLine (a ++ B); // result 666 HC }}}
    


Result: 666 HC is similar to C ++.

 

Generic delegation:

The delegation also has a generic type, and the above example is as follows:

Using System; using System. Collections. Generic; using System. Linq; using System. Text; namespace Example {public delegate void MyDelegate
    
     
(); // Generic delegate class Data
     
      
{Private T a; private T B; public void setvalue (T x, T y) {a = x; B = y ;}// swap method, ref is the public void swap () {T temp; temp = a; a = B; B = temp;} public void printvalue () {Console. writeLine (a ++ B) ;}} class program {static void Main (string [] args) {Data
      
       
Data = new Data
       
        
(); Data. setvalue (HC, 666); MyDelegate
        
         
My = new MyDelegate
         
           (Data. swap); my + = data. printvalue; my (); // The result is 666 HC }}}
         
        
       
      
     
    


Result:

 

 

There are so many introductions about generics, and you are welcome to point out the errors. ^_^

 

Related Article

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.