C#_ commissioned delegate

Source: Internet
Author: User

Commissioned

A delegate in C # (Delegate) is similar to a pointer to a function in C or C + +. A delegate (Delegate) is a reference type variable that holds a reference to a method. References can be changed at run time.

Delegate ( Delegate) is particularly useful for implementing events and callback methods. All delegates (Delegate) are derived from the System.Delegate class.

Declaration Delegate ( Delegate)

A delegate declaration determines the method that can be referenced by the delegate. A delegate can point to a method that has the same label as its.

1 delegate function return type delegate name (< method parameter list >);

instantiate a delegate ( Delegate)

The delegate object must be created using the New keyword and is related to a specific method. When a delegate is created, the arguments passed to the new statement are written like a method call, but without parameters.

1 New delegate type (< registration method >);

Invoking a method through a delegate instance is equivalent to executing the registration method.

anonymous function Initialization delegate

It is cumbersome to specifically define a method for initializing a delegate, typically invoking a method that assigns a value when the delegate instance is initialized, rather than invoking the method itself directly.

The format is as follows:

1 Delegate delegate (function) returns the type delegate type (function argument list); 2 3 New delegate type (delegate(< function parameter list: type parameter name, type parameter name ...>)4{5     // function Body 6 });

or omit the new keyword.

1 Delegate delegate (function) returns the type delegate type (function argument list); 2 3 Delegate (< function parameter list: type parameter name, type parameter name ...>) 4 {5     // function Body 6 };
Delegated Multicast (multicasting of a Delegate)

Delegate objects can be merged using the "+" operator. A merge delegate invokes the two delegates it merges. Only delegates of the same type can be merged. The "-" operator can be used to remove a component delegate from a merged delegate.

Using this useful feature of delegates, you can create a call list of methods to invoke when a delegate is called. This is called a delegated multicast (multicasting), also called multicast.

Multicast can be used to extend or reduce functions like data in a delegate.

1 namespace_9_delegate_2_201708012 {3     classtestdelegate4     {5          Public Static voidPRINTFA ()6         {7Console.WriteLine ("PRINTFA!");8         }9 Ten          Public Static voidPRINTFB () One         { AConsole.WriteLine ("PRINTFB!"); -         } -     } the     Delegate voidmydelegate (); -  -     class Program -     { +         Static voidMain (string[] args) -         { +             //Create a delegate instance A MyDelegate D; atMyDelegate da =Newmydelegate (TESTDELEGATE.PRINTFA); -MyDelegate db =Newmydelegate (TESTDELEGATE.PRINTFB); -D =da; -D + =db; -             //Call Multicast -Console.WriteLine ("Call Delegate instance D ==da+db:"); in d (); -D-=da; toConsole.WriteLine ("Call Delegate instance D ==db:"); + d (); -         } the     } *}
Delegate Multicast Example
Benefits of Delegation

1, the Operation function is more flexible, as the use of variables as convenient, with dynamic , you can avoid the application of a large number of branch statements.

2, compared with the function pointer in C++,c, the delegate is object-oriented, type-safe, reliable controlled object. The delegate can guarantee the address of a safe and effective storage function that does not cross over.

3. The pointer can only point to a static function when compared to a function pointer in C++,c, and a delegate may refer to a static function or a non-static member function .

A delegate can be used when a program must call a method to perform an operation, but the compilation cannot determine what method it is.

C#_ commissioned delegate

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.