C # Anonymous delegate, anonymous function, lambda expression

Source: Internet
Author: User

The relationship between a type, a variable, and an instance.
Type "Variable" example
Types can create variables, entity classes can create instances, and instances can be stored in variables.

Second, the use of the Commission process:
1. Define the delegate (write a good signature);
2. Create a delegate variable;
3. Append the reference function (same signature) to the delegate variable.

Iii. The origin of lambda expressions
private void Button1_Click (object sender, EventArgs e)        {            //anonymous delegate    func<int, int> del            //type ' variable ' instance            //func is an anonymous delegate type, which is an unnamed type            //How to determine its signature: requires several data types as parameters (preceding parameter type, last specified return value type)            //can directly define a delegate variable            // anonymous function    delegate (int a) {return A;};            The difference from the normal function is that the function name is replaced with the delegate            //following expression, which is a variable defined by the anonymous delegate del refers to the process of an anonymous function            //and int a=1, is the same            //func< int, int> del =  delegate (int a) {return A;};            MessageBox.Show (Del (5). ToString ());            Later found that the anonymous function must be placed in the delegate, the parameter type has been determined, so there is a more concise wording, the parameter type is omitted            //There is a lambda expression            func<int, int> del = a + = { return A; };            MessageBox.Show (Del (6). ToString ());        }


Tips:
Define an anonymous delegate variable, like we're sure to build a gun, but the gun doesn't work, because it's just the stage of functional design , the function of the gun is: Give it a bullet (parameter), we get a high-speed flying bullet (return value);
Assigning a lambda expression to this delegate variable is like giving the gun a process of internal construction such as a barrel, a trigger, and then the gun can work.

Iv. events in the WinForm form

WinForm Form event: A variable defined with a predefined delegate (Eventhandle) (receives two parameters).
The form has a delegate variable, and all we do is add the function to the variable.

1.

Add a function to the form Load event

public partial class Form1:form    {public        Form1 ()        {            InitializeComponent ();        }        private void Form1_Load (object sender, EventArgs e)        {        }
An automatically generated expression
Go to definition to see the definition of the Load event

C # Anonymous delegate, anonymous function, lambda expression

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.