In the previous article, we learned about the so-called new syntax for anonymous classes, anonymous methods, and extension methods, which we went on to look at system pre-defined delegates (action/func/predicate) and super-loving lambda expressions. In order to facilitate the yards farmers,. NET base Class Library provides several predefined delegates for the most commonly used scenarios in real-world development, which can be used directly without having to define a delegate type from scratch. Predefined delegates are in the. NET base Class library is widely used, such as in lambda expressions and parallel computing are used extensively, we need to pay attention to it!
/* new Syntax index */ 1. Automatic Properties auto-implemented Properties2. Implicit type Var3. parameter defaults and named Parameters 4. Object initializer and collection initializer {}5. Anonymous classes & anonymous methods 6. Extensions Method 7: The system has built-in delegate FUNC/ACTION8.LAMBDA expression 9. Standard Query operator OPERATOR10.LINQ query expression since the. NET Framework 3.5, a variety of generic delegates have poured in, Some of the delegates that needed to be manually defined by our programmers now have a direct use of predefined delegates, greatly improving development efficiency, and now we'll look at these predefined generic delegates first. A built-in delegate with no return type-action 1.1 the definition given by ACTIONMSDN: Encapsulates a method that does not have parameters and does not return a value. You can use this delegate to pass a method as an argument without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature defined by this delegate. In other words, the encapsulated method must not have parameters and must not return a value. (in C #, the method must return void) Typically, this method is used to perform an operation. Now, let's look at how to use the action delegate: (1) To see first how we used the example of no return value delegate: copy code public delegate void Showvalue (); public class name{ private string instancename; public Name (string name) { This.instancename = name; } public void Displaytoconsole () & nbsp { Console.WriteLine (this.instancename); } public void Displaytowindow () { MessageBox.Show (this.instancename); }} public class testtestdelegate{ public static void Main () { name testname = new name ("Koani"); Showvalue Show Method = testname.displaytowindow; Showmethod (); }} The copy code can clearly see that we have previously explicitly declared a name Showvalue delegate, and assigns a reference to the Name.displaytowindow instance method to its delegate instance. (2) Look at the example of how we came to achieve the above effect after the action delegate: copy code public class name{ private string instancename; public Name (string name) { This.instancename = name; }& nbsp; 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 have to explicitly define a delegate that encapsulates a parameterless procedure. 1.2 deep action in real development, we often pass a delegate instance as a method parameter, so let's take a look at this typical scenario, and then look at what the compiler did to help us do something fun with the reflector anti-compilation tool! (1) First look at the definition of the Foreach method in the List collection type: copy code // //Digest: & nbsp / Perform the specified action on each element of the system.collections.generic.list<t>. // //parametric: // action: / system.action<t> delegate to be executed for each element of system.collections.generic.list<t>. // //exception: // System.argumentnulle xception: // action is null. public void ForeaCH (action<t> Action); The copy code shows that the parameter to the Foreach method is an Action delegate instance, which is a delegate instance with no return value. (2) define an entity class and use the Foreach method with the action delegate: copy code public class person { public int ID {get; set;} public string Name {get; set;} public int age {get; set;} } static void Actiondelegatedemo () { &NBSP;LIST&L T person> personlist = getpersonlist (); personlist.foreach (new action< person> (delegate person p) { Co Nsole. WriteLine (p.id + "-" + P.name + "-" + p.age); }); } Copy the code to see that we are the for Each method passes an instance of an action delegate, which is essentially a method pointer with no return value, and iterates through the output of the information for each person object.
. NET those so-called new syntax Three: System pre-defined delegate and lambda expression