C # Delegate and event and their differences

Source: Internet
Author: User

  1. The delegate can be understood as a method signature.

The method can be used as a parameter of another method for calculation. In C #, there are three ways to create a delegate:

Public Delegate void print (string Str); static void delegatemethod (string Str) {console. writeline (STR);} public static void main () {# region delegate // 1. normal delegate var print1 = new print (delegatemethod); print1 ("this is the delegate created in normal mode"); // 2. anonymous delegate print print2 = delegate (string Str) {console. writeline (STR) ;}; print2 ("this is a delegate created anonymously"); // 3. lambda delegate print print3 = (string Str) => {console. writeline (STR );};
}

  2. Event Events are encapsulated delegates.

It has the following three elements:

1. Event publisher-the object that inspires an event when certain conditions are met

2. Event subscriber-the object to subscribe to the event and process when the event occurs

3. define the relationship between the publisher and the subscriber. A publisher may have multiple subscriber.

  Iii. Differences between events and Delegation

1. Delegation allows direct access to corresponding processing functions through delegation, and events can only be called through published callback functions.

2. events can only use the "+ =" and "-=" methods to register and cancel the subscriber processing functions. In addition to delegation, you can also use the "=" direct value assignment handler function.

Finally, let's take a look at the handling methods of the entire Custom Event and the differences between the event and the delegate as follows:

// Event parameter public class my_eventargs: eventargs {private string _ ARGs = string. empty; Public my_eventargs (string ARGs) {_ ARGs = ARGs;} Public String ARGs {get {return _ ARGs ;}}} // event issuer public class sourceclass {public double width {Get; set;} public double height {Get; set;} my_eventargs evargs; Public sourceclass (string ARGs) {evargs = new my_eventargs (ARGs);} // defines the delegate Public Delegate Vo Id eventhandler (Object sender, my_eventargs ARGs); # region uses the delegate method to Declare Public eventhandler clicked; Public void clickedasync () {If (clicked! = NULL) {clicked (this, evargs) ;}# endregion # region declares the public event eventhandler click using the event method; Public void clickasync () {If (Click! = NULL) {Click (this, evargs) ;}# endregion} // event subscriber public class del {Public Delegate void print (string Str); static void delegatemethod (string Str) {console. writeline (STR);} public static void main () {# region delegate // 1. normal delegate var print1 = new print (delegatemethod); print1 ("this is the delegate created in normal mode"); // 2. anonymous Method
Print print2 = delegate (string Str) {console. writeline (STR) ;}; print2 ("this is the delegate created by the anonymous method"); // 3. lambda delegate print print3 = (string Str) => {console. writeline (STR) ;}; print3 ("this is a delegate created using Lambda"); # endregion # region event sourceclass source = new sourceclass ("My events are triggered "); source. width = 5.0; source. height = 3.0; // 1. The delegate method allows source. clicked (source, new my_eventargs ("ARGs called by Delegate"); Call source. clicked = new sourceclass. eventhandler (source_rightclick); source. clicked + = new sourceclass. eventhandler (source_leftclick); source. clickedasync (); source. clicked (source, new my_eventargs ("ARGs called by Delegate"); // 2. It is obvious that you want to use source. clickedasync (); To call a function, rather than directly calling the upstream code, you need to use the event keyword here to declare // Note 1. the event method does not allow source. clicked (source, new my_eventargs ("ARGs called by Delegate"); direct call // note 2. the event does not allow source. click = new sourceclass. eventhandler (source_leftclick); values are directly assigned to source. click + = new sourceclass. eventhandler (source_leftclick); source. clickasync (); // deregister the event and register the event source. click-= new sourceclass. eventhandler (source_leftclick); source. click + = new sourceclass. eventhandler (source_rightclick); source. clickasync (); console. readline (); # endregion} // event handling method 1 static void source_leftclick (Object sender, my_eventargs ARGs) {sourceclass source = sender as sourceclass; console. writeline ("target width:" + source. width + ", Target Height:" + source. height); console. writeline ("target object parameter:" + args. ARGs);} // event handling method 2 static void source_rightclick (Object sender, my_eventargs ARGs) {sourceclass source = sender as sourceclass; console. writeline ("target area:" + source. width * Source. height );}}

The operation is as follows:

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.