Concept of delegation in C #

Source: Internet
Author: User

A delegate can also be considered as a data type and can be used to define variables. However, it is a special data type, and its Defined variables can only receive a number as a function. More specifically, variables of the delegate type can receive the address of a function, similar to function pointers in C ++. To put it simply, a delegate variable can be regarded as a type of secure function pointer. It can only receive function addresses that meet its requirements. See the following example:

Using System; using System. collections. generic; using System. linq; using System. text; namespace ConsoleApplication1 {class Program {static void Main (string [] args) {MathOptDelegate oppDel; MathOpt obj = new MathOpt (); // The delegate variable receives method references of an object, use oppDel = obj. add; Console. writeLine (oppDel (1, 2) ;}} public class MathOpt {public int Add (int arg1, int arg2) {return arg1 + arg2 ;}// define the delegate type MathOptDelegate, use the keyword delegate public delegate int MathOptDelegate (int value1, int value2 );}

A Delegate is a class derived from Delegate. When a Delegate receives a function, the parameter type and return value of the function must be the same as that defined by the Delegate. As long as this requirement is met, both static and instance can be delegated to it.

In fact, a delegate can not only represent a function, but also combine "a bunch of" functions and then execute them in batches. See the following example:

Using System; using System. collections. generic; using System. linq; using System. text; namespace ConsoleApplication2 {class Program {static void Main (string [] args) {MyDelegate a, B, c, d; // create a delegate object a = MyClass that references the Hello method. hello; Console. writeLine ("Call delegate variable a:"); a ("a"); // create a delegate object that references the Goodbye Method B: B = MyClass. goodbye; Console. writeLine ("Call delegate variable B:"); B ("B"); // combine c, c = a + B With a and B. Console. writeLine ("Call delegate variable c:"); c ("c = a + B "); // c will call two methods in order // remove a from composite delegate c, leaving only B, use d to represent the removal result, d = c-a; Console. writeLine ("Call delegate variable d:"); d ("d = c-a"); // only call Goodbye method:} delegate void MyDelegate (String s ); class MyClass {public static void Hello (String s) {Console. writeLine ("Hello, {0}", s);} public static void Goodbye (String s) {Console. writeLine ("Goodbye, {0}", s );}}}



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.