C # simple example of event proxy

Source: Internet
Author: User

Events and proxies are hard to understand. I have learned some examples from them. Delegate in C # And function pointers in C ++ are basically the same thing, C # implements the function pointer in the form of delegate. The difference is that delegate in C # is type-safe.

Example 1:

Namespace delegatetest
{
Public Delegate void mydelegate ();
Class Test
{
Static void main (string [] ARGs)
{
Changedevent chanevent = new changedevent ();
Eventlistener eventlis = new eventlistener (chanevent );
Chanevent. onchanged ();



Console. Read ();
}
}
// Define a class that contains event members. An event is triggered when a change occurs.
Public class changedevent
{
Public event mydelegate changed;
Public void onchanged ()
{
If (changed! = NULL)
Console. writeline ("trigger event :");

Changed ();
}

}

// Create a class containing the processing method
Class eventlistener
{
// It is used to save the objects passed from the parameter of the constructor culvert, mainly to unbind the event in the future
// Private changedevent CE;

Public eventlistener (changedevent CE)
{
// Ce = Ce;
Ce. Changed + = new mydelegate (dispmethod );
}
Private void dispmethod ()
{
Console. writeline ("this is called when the event fires .");

}

}
}

Example 2:

Namespace delegate2
{

Public Delegate void mydelegate ();
Class Test
{

Static void main (string [] ARGs)
{
Changedevent chanevent = new changedevent ();
A A = new ();
Chanevent. Changed + = new mydelegate (A. dispmethod );
Chanevent. onchanged ();



Console. Read ();
}
}
Class
{

Public void dispmethod ()
{
Console. writeline ("this is called when the event fires .");

}
}

// Define a class that contains event members. An event is triggered when a change occurs.
Public class changedevent
{
// Public Delegate void mydelegate ();

Public event mydelegate changed;
Public void onchanged ()
{
If (changed! = NULL)
{
Console. writeline ("trigger event :");

Changed ();
}
}

Example 3: Delegate with Parameters

Namespace consoleapplication1
{
// Define the delegate
Public Delegate bool compareint (int A, int B );
Public class mycompare
{
Public static bool showresult (int x, int y)
{
Bool Index = x> Y? True: false;
Return Index;
}

}
Class Program
{

Static void main (string [] ARGs)
{
Compareint mydelegate = new compareint (mycompare. showresult );
Int A = 20;
Int B = 15;
Bool whobigger = mydelegate (A, B );
Console. writeline ("A> B? : "+ Whobigger );
Console. Read ();
}
}
}

 

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.