C # Delegate and merge delegate (multi-channel broadcast delegate)

Source: Internet
Author: User

Delegate is a type of secure encapsulation method, which is similar to function pointers in C and C ++. Unlike function pointers in C, delegation is object-oriented, type-safe, and insurance. The delegate type is defined by the delegate name. The following example declares a delegate named Del, which can encapsulate a method that uses strings as parameters and returns void. [Csharp] public delegate void Del (string message); when constructing a delegate object, the name of the delegate method to be wrapped or the anonymous method is usually provided. After the delegate is instantiated, the Delegate will pass the method calls to it to the method. The parameters passed by the caller to the delegate are passed to the method. The return values (if any) from the method are returned by the delegate to the caller. This is called a call delegate. An instantiated delegate can be called as the wrapped method itself. For example: [csharp] // Create a method for a delegate. public static void DelegateMethod (string message) {System. console. writeLine (message);} This example shows how to combine multicast delegates. A purpose of the delegate object is that you can use the + operator to assign them to a delegate instance to be a multicast delegate. The combination of the two delegates can be called. Only delegates of the same type can be combined. -Operators can be used to remove component delegation from the composite delegate. [Csharp] class Class1 {public delegate void PrintDelegate (string name, string id); public delegate void Del (int I); static void Main (string [] args) {Class1 obj = new Class1 (); PrintDelegate delegate1 = PrintStudent; PrintDelegate delegate2 = obj. printeBook; // + the object operated by the operator can only be a delegate object. // PrintDelegate multiDel = delegate1 + delegate2; // But + = the operation object on the right can be a method signature. // Simple multicast delegation. PrintDelegate multiDel = null; multiDel + = obj. printeBook; multiDel + = PrintStudent; multiDel ("test", "123"); Del d = delegate (int I) {Console. writeLine (++ I) ;}; d (10); Console. read ();}/*** both static and non-static methods can be used for delegation and signature methods. ** // Static method public static void PrintStudent (string name, string id) {www.2cto.com Console. writeLine ("student information"); Console. writeLine ("student id: {0}", id); Console. writeLine ("name: {0}", name) ;}// non-static method. Public void PrinteBook (string bookName, string code) {Console. writeLine ("books"); Console. writeLine ("No.:" + code); Console. writeLine ("name:" + bookName );}}

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.