Delegate, anonymous method, Lambda, generic delegate, expression tree

Source: Internet
Author: User

First, the Commission:
The completion of a delegate should be in three steps:
STEP01: First define a delegate with delegate;
public delegate int Calculatoradd (int x, int y);
STEP02: Declares a method to correspond to a delegate.
public int Add (int x, int y)
{
return x + y;
}
protected void Frmtest_load (object sender, EventArgs e)
{
STEP03: Use this method to instantiate the delegate.
Calculatoradd cAdd = new Calculatoradd (ADD);
int result = CADD (5, 6);
int result = Cadd.invoke (5,6);
}
STEP01: First define a delegate with delegate.
STEP02: Declares a method to correspond to a delegate.
STEP03: Use this method to instantiate the delegate.
At this point, a delegate should be completed, you can invoke the delegate.
An event is an instance of a delegate, which is not mentioned here.

Second, anonymous method:
The anonymous method is used to simplify the upper three-step syntax for sugar.
STEP01: First define a delegate with delegate;
public delegate int Calculatoradd (int x, int y);
protected void Frmtest_load (object sender, EventArgs e)
{
STEP02: Assigning a method to a delegate
Calculatoradd cAdd = delegate (int x, int y) {return x + y;};
int result = CADD (5, 6);
int result = Cadd.invoke (5,6);
}
STEP01: First define a delegate with delegate.
STEP02: Assigning a method to a delegate is, in fact, an anonymous method.

Third, lambda expression:
Microsoft's design philosophy for C # is simple to use, and once again the anonymity method is simplified, and lambda appears with several lambda expressions.
public delegate int Calculatoradd (int x, int y);
protected void Frmtest_load (object sender, EventArgs e)
{
Method One:
Calculatoradd CADD1 = (int x, int y) = = {return x + y;};
int result1 = CADD1 (5, 6);

Method Two:
Calculatoradd CADD2 = (x, y) = = {return x + y;};
int result2 = CADD2 (5, 6);

Method Three:
Calculatoradd cAdd3 = (x, y) + x + y;
int RESULT3 = CADD2 (5, 6);
}
Method One: Simply remove the delegate and add "=" between () and {};
Method Two: The parameter type is eliminated on the basis of method one;
Method Three: On the basis of method two, {}, and return keywords are removed;

Four, generic Commission:
Whether it is an anonymous method or a lambda expression, the application of a delegate can be completed in two steps, one step defining the delegate, and the other using a method to instantiate a delegate.
The generic delegate synthesizes the two steps. Use Func to simplify the definition of a delegate, where Func is the so-called generic delegate. If the delegate function returns void, the func<> is replaced with Action<>.
protected void Frmtest_load (object sender, EventArgs e)
{
Method One:
func<int,int,int> CADD1 = (int x, int y) = = {return x + y;};
int result1 = CADD1 (5, 6);

Method Two:
Func<int, int, int> CADD2 = (x, y) = = {return x + y;};
int result2 = CADD2 (5, 6);

Method Three:
Func<int, int, int> CADD3 = (x, y) + x + y;
int RESULT3 = CADD3 (5, 6);
}

Five, expression tree:
An expression tree is a container that holds delegates, or a data structure that accesses lambda expressions. To use a lambda expression, get it directly from the expression, Compile () can be used directly.
protected void Frmtest_load (object sender, EventArgs e)
{
Expression<func<int, int, int>> exp = (x, y) + x + y;
Func<int, int, int> fun = Exp.compile ();
int result = Fun (2, 3);
}

Delegate, anonymous method, Lambda, generic delegate, expression tree

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.