Use the Demo to take a closer look at the internal mechanism of the C # event

Source: Internet
Author: User

 

So here I will use the C # event to describe the Delegate (Delegate ). Let's take a look at the sample code: Code public delegate void ProcessEventHandler (string name );
Public class CharlesLog
{
Public event ProcessEventHandler processEvent;
Public void ProcessHandler (string name)
{
If (processEvent! = Null)
{
ProcessEvent (name );
}
Else
{
ProcessEvent = new ProcessEventHandler (DefaultHandler );
ProcessEvent (name );
}
}
Public void DefaultHandler (string name)
{
Console. WriteLine ("default Process! ");
}
}
Class Program
{
Static void Main (string [] args)
{
CharlesLog charlesLog = new CharlesLog ();
CharlesLog. processEvent + = new ProcessEventHandler (CustomProcess );
CharlesLog. ProcessHandler ("CharlesChen ");
}
Public static void CustomProcess (string name)
{
Console. WriteLine ("custom Process! ");
}
}
According to this Demo, some important points need to be paid attention to for delegation and events. I will list them here:1. The above delegate instantiation is. net 1.1 Syntax: (Note: As the Net version is upgraded, the syntax for delegation instantiation is simplified. In 2.0, there are anonymous methods and 3.0 Lambda expressions that are more in line with human languages. CharlesLog. processEvent + = new ProcessEventHandler (CustomProcess); 2. Statement event Syntax: "modifier & r

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.