The connection and difference between events and delegation, and the difference between event Delegation

Source: Internet
Author: User

The connection and difference between events and delegation, and the difference between event Delegation
Connection and difference between events and Delegation

-An event is a special delegate, or a restricted delegate. It is a special application that can only be used with the + =,-= Operator. The two are essentially one thing.

-Event ActionHandler Tick; // compile it to create a private delegate example, and add and remove methods applied to it.

-The "add" and "remove" methods are only allowed to be used for event operations. As a result, the event cannot be directly triggered outside the class and can only be triggered at a suitable time within the class. The delegate can be triggered externally, but not in this way.

-In use, the delegate is often used to express the callback and the event expresses the outbound interface.

-The delegate and event support static and member methods. When delegate (void * pthis, f_ptr) supports static return methods, pthis is null. when the member method is supported, pthis transmits the notification object.

-The three important fields in the delegate object are pthis, f_ptr, and pnext, which are referenced by the notification object, function pointer/address, and the next delegate node of the delegate linked list.

 

1. How to define the delegate type?

Delegate + function signature // delegate is equivalent to the class keyword, and function name is equivalent to the defined delegate type.

Delegate void Mydelegate ();

2. How to define a delegated instance? Mydelegate my; (this is a simple delegate definition without return values );

3. How to define events?

First, define the delegate type delegate void ActionHandler (object sender, EventArgs args). In fact, this parameter is optional.

Define event ActionHandler Tick;

 

Memory method:

The delegate keyword is similar to the class keyword. It defines a delegate type and creates a delegate example.

When you create a delegated instance, modifying it with the event keyword becomes an event, that is, an event is a special delegate.

 

 1 class MyClass 2     { 3         public delegate void CompletedEventHandler(); 4  5         public event CompletedEventHandler WorkCompleted; 6         public CompletedEventHandler WorkCompletedDelegate; 7  8         public void Fire() 9         {10             if (this.WorkCompleted != null)11             {12                 this.WorkCompleted();13             }14 15             if (this.WorkCompletedDelegate != null)16             {17                 this.WorkCompletedDelegate();18             }19         }20     }21     class Program22     {  23         static void TestEvent()24         {25             Console.WriteLine("test event");26         }27 28         static void TestDelegate()29         {30             Console.WriteLine("test delegate");31         }32 33         static void Main(string[] args)34         {35 36             MyClass myObject = new MyClass();37             myObject.WorkCompletedDelegate += TestDelegate;38             myObject.WorkCompleted += TestEvent;39   }

Reprinted to http://www.cnblogs.com/?let/archive/2013/09/15/3247020.html;

After reading this article, in a look at the role of the previous event and use will feel, http://www.cnblogs.com/cn-blogs/p/3413652.html


C # What is the difference between events and delegation?

An event is a narrow delegate, that is, an event is a dedicated delegate used for the event-driven model.

In layman's terms, you can directly call the delegate in the client code to stimulate the Delegate-directed function, but the event cannot be triggered. The event can only be triggered by the Service Code itself.

That is to say, in your code, entrusting you not only can arrange who calls the function, but also can directly call it, and events cannot be called directly, but can only be triggered through some operations.

You can understand that an event is one or more delegates. In this case, it should be incorrect. An event can have multiple event processing functions, and a delegate can also be a multicast delegate.

C # Delegate and event differences

An event is implemented by a delegate. Of course, a delegate can replace an event. An event has two more private methods than the Delegate-add and remove. You can write an event by yourself, and then use the decompilation tool to see what is going on.

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.