Delegate, Lambda expression, event series 01, Delegate, basic delegate usage, delegate Method and Target attribute, lambdatarget
A delegate is a class.
namespace ConsoleApplication1
{
internal delegate void MyDelegate(int val);
class Program
{
static void Main(string[] args)
{
}
}
}
Use Reflector to view the delegate IL code:
○ Delegate is indeed a class
○ Delegate constructor receiver method and class instance
○ It is also a multicast delegate and can be assigned a value using ++ =.
○ Delegate internal use of the Invoke Method for triggering
○ BeginInvoke and EndInvoke methods are used in multithreading scenarios.
Next, we will experience how to use the delegate and what the delegate's Method and Target attributes represent.
namespace ConsoleApplication1
{
internal delegate void MyDelegate(int val);
class Program
{
static void Main(string[] args)
{
// Delegate and static methods
MyDelegate d = new MyDelegate(M1);
d(10);
Console.WriteLine(d.Method);
if (d.Target == null)
{
Console. WriteLine ("The current delegate calls static methods without class instances ");
}
else
{
Console. WriteLine ("The current delegate calls an instance method, and the class instance is:" + d. Target );
}
Console.WriteLine("-------------------");
// Delegate and instance methods
Program p = new Program();
d = p.M2;
d.Invoke(10);
Console.WriteLine(d.Method);
if (d.Target == null)
{
Console. WriteLine ("The current delegate calls static methods without class instances ");
}
else
{
Console. WriteLine ("The current delegate calls an instance method, and the class instance is:" + d. Target );
}
}
static void M1(int val)
{
Console. WriteLine ("I Am a static method, output" + val );
}
void M2(int val)
{
Console. WriteLine ("I am an instance method, output" + val );
}
}
}
○ Create a delegate: Use the Delegate constructor, new MyDelegate (M1), or d = p. m2, which is a method of writing "syntactic sugar", also calls the Delegate constructor internally.
○ Delegate and static methods: You can pass the static method to the Delegate constructor as long as the parameter list and return type are the same.
○ Delegate and instance methods: You can pass the instance method to the Delegate constructor as long as the parameter list and return type are the same.
○ Delegated call: for example, d. invoke (10), called through the entrusted instance method Invoke; like d (10), this is a "syntax sugar" method, which also calls the instance method Invoke internally.
○ Target attribute: indicates the name of the class instance to which the instance method belongs. If it is a static method, Target is null.
○ Method attribute: The Method represented by the delegate, which may be a static Method or an instance Method.
"Delegation, Lambda expressions, and event series" include:
Delegate, Lambda expression, event series 01, Delegate, basic delegate usage, delegate Method and Target attribute delegate, Lambda expression, event series 02, when should I use delegate, Lambda expression, event series 03, from delegate to Lamda expression?
Lambda expressions are not understood. Here is a simple example,
The biggest function is to use anonymous functions and linq queries.
This is used on the anonymous method:
Delegate int del (int I );
Del myDelegate = x => x * x;
Int j = myDelegate (5); // j = 25
Equivalent
Delegate int del (int I );
Del myDelegate = delegate (int I) {I = I * I ;};
Int j = myDelegate (5); // j = 25
As for the future of linq, do not go into details.
Direct I + 1 ???
Haha, you have not encountered some situations that must be delegated.
For example, for cross-thread calls, you can only use delegates, while lambda expressions are a very convenient way of writing. This is purely for convenience.
How to Use lambda in C # To handle delegate events
The delegate is defined as follows: copy the code as follows: public class SocketSp {public delegate void ReceiveCompleted (byte [] receiveBuffer, int etetotallen, Exception ex); public ReceiveCompleted receiveCompleted ;} the following copy code is defined by the Caller: public class LinkOuter {SocketSp linkOuterSocket = new SocketSp (); private void test (Socket requestHandleSocket) {// link linkOuterSocket here. receiveCompleted event. You also want to pass in the requestHandleSocket parameter for later processing .}} The first idea was to use delegate but failed. Although it is attached, the parameter passed by the delegate is lost and subsequent operations cannot be performed. Copy the Code as follows: private void test (Socket requestHandleSocket) {linkOuterSocket. receiveCompleted + = delegate {// To do};} The second idea is To use Action, and the result also fails. The IDE prompts that the delegated Action does not use three parameters. The copy code is as follows: private void test (Socket requestHandleSocket) {linkOuterSocket. receiveCompleted + = (Action) (outerReceiveBuffer, totalLen, ex) =>{// To do});} The third idea is To use lambda expressions To First connect To the delegate, at the same time, the call of local variables is used to transmit parameters to the sendResponse function for subsequent operations. The copy code is as follows: private void test (Socket requestHandleSocket) {linkOuterSocket. receiveCompleted + = new SocketSp. receiveCompleted (outerReceiveBuffer, totalLen, ex) => {byte [] realOuterReceiveBuffer = new byte [totalLen]; Array. copy (outerReceiveBuffer, 0, realOuterReceiveBuffer, 0, totalLen); sendResponse (requestHandleSocket, realOuterReceiveBuffer, "200 OK", "text/html ");});} finally, it is implemented using lambda expressions. Articles that you may be interested in: C # Use Lambda and delegate template Methods