The so-called new syntax 3 in. NET: The system predefines the delegate and Lambda expression,. netlambda

Source: Internet
Author: User

The so-called new syntax 3 in. NET: The system predefines the delegate and Lambda expression,. netlambda

In the previous article, we learned about the so-called New syntaxes such as anonymous classes, anonymous methods, and extension methods. In this article, we will continue to look at the system's pre-defined delegate (Action/Func/Predicate) lambda expressions of Hao. For convenience ,. the Net-based Class Library provides several predefined delegates for the most common scenarios in actual development. These delegates can be used directly without the need to define their own delegate types. Predefined delegates are widely used in. Net base class libraries. For example, they are widely used in Lambda expressions and parallel computing. we need to pay attention to them!

/* New syntax Index */1. auto-Implemented Properties2. implicit var3. default value and name parameter 4. object initializer and set initializer {} 5. anonymous class & anonymous method 6. extension Method 7. the system has built-in delegate Func/Action8.Lambda expression 9. the Standard Query Operator10.LINQ Query expression is from. since the NET Framework 3.5, a variety of generic delegation have emerged. Some of the previously defined delegation needs to be manually defined by our programmers. Now we can directly use the predefined delegation, which greatly improves the development efficiency, now let's take a look at these predefined generic delegation. 1. built-in delegate-Action 1.1 with no return type first recognized the definition given by ActionMSDN: encapsulate a method that does not have parameters and does not return values. You can use this delegate to pass methods as parameters without explicitly declaring custom delegates. The encapsulated method must correspond to the method signature defined by the delegate. That is to say, the encapsulated method cannot have parameters and cannot return values. (In C #, this method must return void.) generally, this method is used to execute an operation. Now let's take a look at how to use the Action delegate: (1) first, let's take a look at how we used the non-return value delegate example: copy the code public delegate void ShowValue (); public class Name {private string instanceName; public Name (string name) {this. instanceName = name;} public void DisplayToConsole () {Console. writeLine (this. instanceName);} public void DisplayToWindow () {MessageBox. show (this. instanceName) ;}} public class testTestDelegate {public static void Main () {Name te StName = new Name ("Koani"); ShowValue showMethod = testName. displayToWindow; showMethod () ;}} the copy Code clearly shows that we need to explicitly declare a delegate named ShowValue before, and. A reference to the DisplayToWindow instance method is assigned to its delegated instance. (2) Let's take a look at the example of how we achieved the above effect with the Action delegate: copy the code public class Name {private string instanceName; public Name (string name) {this. instanceName = name;} public void DisplayToConsole () {Console. writeLine (this. instanceName);} public void DisplayToWindow () {MessageBox. show (this. instanceName) ;}} public class testTestDelegate {public static void Main () {Name testName = new Name ("Koani"); Action showMethod = TestName. DisplayToWindow; showMethod () ;}} the copy Code clearly shows that when you use the Action delegate, you do not need to explicitly define a delegate to encapsulate the process without parameters. 1.2 in-depth Action in actual development, we often pass a delegated instance as a method parameter. So let's take a look at this typical scenario, use Reflector's decompilation tool to check what the compiler has done for us! (1) first, let's take a look at the definition of the ForEach method in the List set type: copy the code // Abstract: // to the System. collections. generic. each element of List <T> performs the specified operation. //// Parameter: // action: // System. Action <T> delegate to each element of System. Collections. Generic. List <T>. //// Exception: // System. ArgumentNullException: // action is null. Public void ForEach (Action <T> action); copy the code and you can see that the parameter of the ForEach method is an Action delegate instance, that is, a delegate instance with no return value. (2) define an object class and use the ForEach method through the Action delegate: copy the code public class Person {public int ID {get; set;} public string Name {get; set ;} public int Age {get; set ;}} static void ActionDelegateDemo () {List <Person> personList = GetPersonList (); personList. forEach (new Action <Person> (delegate (Person p) {Console. writeLine (p. ID + "-" + p. name + "-" + p. age) ;}) ;}copy the code and we can see that we passed an Action delegate instance for the ForEach method, essentially a non-return value. Returns the information of each Person object.

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.