C # delegated learning Diary

Source: Internet
Author: User
I once had a headache for delegation and events. Fortunately, now I am getting started in my own way. I just want to repeat it in my own way as a learning diary.

A delegate is a type of reference. We should treat it as a class rather than a method when processing it. To put it bluntly, a delegate is a reference to the method or method list, calling a delegate instance is like calling a pointer in c ++. It encapsulates the reference to the method, or the delegate serves as a bridge, the delegate object after the instance passes the given parameters to the method called back by the user and executes the method.

Let's look at a simple example:

// Declare a delegate
Delegate int myDelegateHandler (int a, int B );

Public class
{
// Static Processing Method
Public static int M1 (int a, int B)
{
Int c = 0;
C = a + B;
Return c;
}
}
// Entry class
Public class B
{
Public static void Main ()
{
// A delegate to the instance
MyDelegateHandler mdh = new myDelegateHandler (A. M1 );
// Call the delegate
Int sum = mdh (2, 2 );
Console. WriteLine (sum. ToString ());

}
}

The above is a very simple example. The specific implementation steps are as follows:

1. Declare a delegated instance first;

2. then provide the method to be processed;

3. instantiate the delegate (if the delegate is regarded as a class, it is not difficult to understand the instantiate delegate. Its parameter is the method to be processed. The method here does not need to be enclosed in brackets, the instantiation process is the process of loading methods, just like the constructor that requires parameters.) The instantiated delegate object is like a pointer in c ++, it is an object that encapsulates methods;

4. Finally, we call the delegate object to call the encapsulated method itself, and the parameters for the call are passed to the encapsulated method.

5. Note that the declared delegate, regardless of the number of parameters, parameter type, and return value type, must be consistent with the method to be encapsulated. When the delegate instance object is called, the input parameters must be consistent. Otherwise, an error occurs.

 

 

Delegated chain

We know that delegation encapsulates methods and can encapsulate many methods to form a delegation chain. In fact, delegation is like a container, which encapsulates several methods we want to implement, when a delegate object (equivalent to a pointer in c ++) is called, all the methods encapsulated by the delegate object are executed sequentially. If a return value exists, the Return Value of the last executed method is often returned. You can use "+ =" or "-=" to perform binary operations on different delegated instances.

Delegated link instance:

// Define a delegate
Public delegate void PrintHandler (string message );

Public class PrintProvider1
{
Public void Print (string msg)
{
Console. WriteLine (msg + "1111111 ");
}

}

Public class PrintProvider2
{
Public void Print (string msg)
{
Console. WriteLine (msg + "2222222 ");
}

}

Class Delegate1
{
Public static void StaticPrint (string msg)
{
Console. WriteLine (msg + "3333333 ");

}

Public static void Main ()
{
String s = "delegate chain ";
PrintProvider1 pp1 = new PrintProvider1 ();
PrintProvider2 pp2 = new PrintProvider2 ();
// Create a delegated instance
PrintHandler prn1 = new PrintHandler (pp1. Print );
PrintHandler prn2 = new PrintHandler (pp2. Print );
PrintHandler prn3 = new PrintHandler (StaticPrint );

Console. WriteLine ("print result of delegated instance prn1 ");
Prn1 (s );

Console. WriteLine ("print result of delegated instance prn2 ");
Prn2 (s );

Console. WriteLine ("print result of delegated instance prn1 + prn2 ");
PrintHandler prn = prn1 + prn2;
Prn (s );

Console. WriteLine ("print result of delegated instance prn1 + prn2 + prn3 ");
Prn + = prn3;
Prn (s );

Console. WriteLine ("print result of delegated instance prn1 + prn3 ");
Prn-= prn2;
Prn (s );

Console. WriteLine ("print result of delegated instance prn3 ");
Prn-= prn1;
Prn (s );

Console. WriteLine ("an exception is thrown when a null delegate is attempted ");
Try
{
Prn-= prn3;
Prn (s );

}
Catch (NullReferenceException ex)
{
Console. WriteLine (ex. Message );
}

Console. WriteLine ("it is invalid to try to remove delegation from null ");
Try
{
Prn-= prn3;
}
Catch (NullReferenceException ex)
{
Console. WriteLine (ex. Message );

}

Console. Read ();

}

}

Execution result

Print result of delegated instance prn1
Delegated chain 1111111
Print the result of the delegated instance prn2
Delegated chain 2222222
Printed result of delegated instance prn1 + prn2
Delegated chain 1111111
Delegated chain 2222222
Printed result of delegated instance prn1 + prn2 + prn3
Delegated chain 1111111
Delegated chain 2222222
Delegated chain 3333333
Printed result of delegated instance prn1 + prn3
Delegated chain 1111111
Delegated chain 3333333
Print the result of the delegated instance prn3
Delegated chain 3333333
An exception occurs when you try to call a null delegate.
You have not set the object reference to the instance of the object.
It is invalid to try to remove the delegate from null.

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.