C #-delegation and events

Source: Internet
Author: User

A few days ago, the director told me about some entrusted things. Today I will summarize them here.

 


Delegate:

In C #, everything is an object. An integer can point to it with an integer variable. An object can point to it with an object variable, or a function can point to it with a delegate variable.


How to declare delegation: delegate return value type delegate type name (parameter)

For example: delegate string DeleGet (sring att)

Note: Except for the delegate keyword delegate, the return value type (the first string in the example) and parameter (string att in the example) must be consistent with the delegate function. DeleGet is the delegate type name, not the function name

The declared delegate is a type, which is the same as int and string. If you want to use it, you also need to declare the delegate class variable. The way to declare the delegate type variable is DeleGet f1;

Direct the delegate variable to the function: DeleGet f1 = new DeleGet (SayHello); (or the abbreviation DeleGet f1 = SayHello;), so that f1 can be used as a function just like a common function. The delegate can be regarded as a pointer to a function.

After adding a function to a delegate variable, you can add multiple ones: f1 + = SayBay;, which forms a delegate chain, when this delegate variable is executed, the delegate variable points to one or more methods. The same f1-= SayBay; is to remove the methods in the delegate variable.

 


Event:

Event Syntax: event ProcessWordDelegate OnWord;


The benefits of implementing the event mechanism with the event Keyword: when an event is used, the registered values of the event cannot be modified, and the event notification cannot be impersonated. You cannot use OnIntInput (3) to call the registered delegate outside the IntUC class. Only + =,-=! Comparing the differences between "Event 1" and "Event 2", we can see the differences between "events and delegation!

 

 

The delegate and event are not comparable, because the delegate is a type, and the event is an attribute (object). The following describes the delegate object (events implemented using the delegate method) and (implemented in standard event mode) events. Internal events are implemented by delegation.
For an event, you can only register yourself + = or deregister yourself-=. other registrants cannot be deregistered, or the event cannot be triggered, therefore, if Delegate is used, the above control cannot be implemented, so the event syntax is born.
The event is used to caster the delegated instance, similar to a custom class castrated List. Events can only be added or removed, but cannot be assigned values. Event can only be + =,-=, cannot =


Event will automatically generate a delegate variable and two functions: add and remove. C # the compiler uses these two methods to support the + = and-= operators (*)
Public event MyDelegate OnEvent;
Internal implementation is (not strictly explained)
Private MyDelegate OnEvent;
Public void Add (MyDelegate d)
{
OnEvent + = d;
}
Public void Remove (yDelegate d)
{
OnEvent-= d;
}
Because OnEvent is private, OnEvent (1) cannot be triggered outside the class, but it can be used inside the class.
Public methods only include Add and Remove, so only + = and-= are allowed. Other operations are not allowed.


 

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.