Example of generic class/generic delegation

Source: Internet
Author: User
Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. text;
Using System. Windows. forms;

Namespace Deletegatet
{
Public   Partial   Class Form1: Form
{
Public Form1 ()
{
Initializecomponent ();
}

Private   Void Button#click ( Object Sender, eventargs E)
{
Myclass < String , String > _ Myclass =   New Myclass < String , String > (); // Instantiate a generic class myclass
_ Myclass. _ mydelegate =   New Mydelegate < String , String > (Demomethod ); // Instantiation _ generic delegation of myclass
MessageBox. Show (_ myclass. getvalues ( " Jimmy's book " , " Jimmy " )); // At this time, the compiler can determine that the parameter type is string, and then call the generic method demomethod by Delegate.


_ Myclass. _ myevent + =   New Myevent < String , String > (Demoevent < String , String > ); // Subscribe to an event for _ myclass
_ Myclass. invokeevent ( " Jimmy " , " China " ); // Event triggering
}


// Define a delegate with a return value
Public   Delegate   String Mydelegate < T, s > (T title, s author );

// Define event delegation.
Public   Delegate   Void Myevent < E, P > (E name, P address );

Public   Class Myclass < V, F >
{
// Delegate Declaration
Public Mydelegate < V, F > _ Mydelegate;
// Declare event Delegate
Public   Event Myevent < V, F > _ Myevent =   Null ;

Public   String Getvalues (V title, F author)
{
//Call delegate
Return_ Mydelegate (title, author );
}

Public Myclass ()
{
}

Public   Void Invokeevent (V name, F address)
{
If (_ Myevent ! =   Null )
{
//Call delegate
_ Myevent (name, address );
}
}
}


Public   String Demomethod < T, s > (T title, s author)
{
ReturnTitle. tostring ()+ ", Author:" +Author;
}

Private   Void Demoevent < V, F > (V name, F address)
{
MessageBox. Show (name+ "Come from" +Address );
}
}



}

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.