Summary of multicast delegate notes in C #

Source: Internet
Author: User
Definition :
Delegate It is a type of method reference stored in an object, and also a type of safe function pointer.
One way to understand delegation can regard the role of delegation as specifying a name for the method signature.
The delegate definition is similar to the method definition, but there is no method body. The keyword delegate must be added before the defined delegate name.

Because the definition delegate basically defines a new class, you can define the delegate anywhere in the definition class, either in the internal definition Delegate of another class, you can also define the delegate as a top-level object in the namespace. According to the defined visibility, you can add general access modifiers: public, private, and protected on the delegate definition:

A delegated instance is compressed (or encapsulated) by a method called "An callable entity ", therefore, "An callable object" is composed of the delegated instance itself and the method encapsulated in the instance.

A delegate can also contain multiple methods. This delegate is called a multicast delegate.

If you call multicast delegates, you can call multiple methods in sequence. Therefore, the delegate signature must return void (otherwise, where should the return value be sent ?) (When a delegate only contains one method, its return type declaration can refer to the encapsulated method, not necessarily void ). In fact, if the compiler finds that a delegate returns void, it automatically assumes that this is a multicast delegate.

Advantages of delegation:
(1) Call the compression method. (2) reasonable and effective use of delegation can improve applications Program Performance. (3) It is used to call an anonymous method.

Delegate statement:Scope modifier delegate return type delegate name ();
For example, public delegate void test (). Note: You can declare a delegate without parameters or parameter lists. The delegate should be declared in the same syntax as the declarative method.

Example program of delegation: Public Delegate double delegate_prod (int A, int B );
Class class1
{
Static double fn_prodvalues (INT val1, int val2)
{
Return val1 * val2;
}
Static void main (string [] ARGs)
{
// Creating the delegate instance
Delegate_prod delobj = new delegate_prod (fn_prodvalues );
Console. Write ("Please enter values ");
Int V1 = int32.parse (console. Readline ());
Int v2 = int32.parse (console. Readline ());
// Use a delegate for processing
Double res = delobj (V1, V2 );
Console. writeline ("Result:" + Res );
Console. Readline ();
}
}

Explanation: I used a small program to demonstrate the use of delegation. Two return types that only accept integer variables are specified during the delegate delegate_prod declaration. The same applies to methods named fn_prodvalues in the same class. The delegate and method have the same signature and parameter type. Create a delegate instance in the main method and pass the function name to the delegate instance in the following way: delegate_prod delobj = new delegate_prod (fn_prodvalues ); in this way, we accept two values from the user and pass them to the delegate: delobj (V1, V2 ); the delegate object compresses the function of the method and returns the result we specified in the method.

Multicast delegation:(1) multicast delegates include references to more than one method. (2) The method contained in the multicast delegate must return the void; otherwise, a run-time exception is thrown.
Example program for multicast delegation:
Using system;
Using system. Collections. Generic;
Using system. text;

Namespace consoleapp1
{

// Declare a delegate
Public Delegate void MSG ();

// Write a class
Class messges
{
// A member method M1 in the messges class
Public static void M1 ()
{Console. writeline ("method 1 called by huashanlin ");}

// A member method m2 in the messges class
Public static void m2 ()
{Console. writeline ("method 2 called by huashanlin ");}

// A member method m3 in the messges class
Public static void m3 ()
{Console. writeline ("method 3huashanlin called ");}
}

// Another class
Class Program
{

// Main functions contained in this class
Static void main (string [] ARGs)
{
// Instantiate a delegate and encapsulate a method m2 in the messges class
Msg ms = new MSG (messges. m2 );

// Encapsulate a new method M1 in the original delegated instance that encapsulates an M1 Method
MS = MS + messges. M1;
// Or the preceding statement can be written as MS + = messges. M1; the effects are the same.

// Encapsulate the third method for the delegated instance
MS + = messges. m3;

// Call the delegate instance. Therefore, after the multicast delegate is called, the three methods encapsulated in the delegate are executed.
MS ();

}
}
}

// Note: The method signature saved by deletge exists in the form of a hash queue.

Example Program 2 of multicast delegation:

Delegate void delegate_multicast (int x, int y ); Class class2 {
Static void Method1 (int x, int y ){
Console. writeline ("you r in method 1 ");
}
Static void method2 (int x, int y ){
Console. writeline ("you r in method 2 ");
}
Public static void main ()
{
Delegate_multicast func = new delegate_multicast (Method1 );
Func + = new delegate_multicast (method2 );
Func (1, 2); // Method1 and method2 are called
Func-= new delegate_multicast (Method1 );
Func (2, 3); // only method2 is called
}
} Resolution: The preceding example defines two methods, namely, Method1 and method2, that accept Integer Parameters and return void. In the main function, use the following statement to create the delegate object: delegate_multicast func = new delegate_multicast (Method1); then use + = to add the delegate and use-= to remove the 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.