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. Place the function (the same signature) in the delegate variable.

Event: A variable defined with a pre-defined delegate (Eventhandle) (receives two parameters).
The form has a delegate variable, and all we do is add the function to the variable.

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
Iii. The origin of lambda expressions
Private voidButton1_Click (Objectsender, EventArgs e) {            //Anonymous delegate func<int, Int> del//type "Variable" example//Func is an anonymous delegate type and is a type with no name//how to determine its signature: Several data types are required as parameters (previous specified parameter type, last specified return value type)//a delegate variable can be defined directly//anonymous function delegate (int a) {return A;}; //The difference from the normal function is that the function name is replaced with the delegate//The following expression, a variable that is defined by an anonymous delegate, refers to an anonymous function//The process of a=1 with int is the same truth//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 also omitted//There's a lambda expression.func<int,int> del = a = = {returnA;}; MessageBox.Show (Del (6).        ToString ()); }

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.