C # Use of Action and Func,

Source: Internet
Author: User

C # Use of Action and Func,

In the daily use of delegate, we usually need to display the declaration of a delegate named XXX, and when using the Action delegate, do not display and define a delegate that encapsulates the process without parameters.

For example, if delegate is used normally:

1 using System; 2 3 namespace MT 4 {5 public delegate void ShowValue (); // display the declaration of a delegate 6 7 public class Test 8 {9 // here there is a Test class, the class has a void method, the function is to output a string 10 private string instanceName; 11 public Test (string name) 12 {13 this. instanceName = name; 14} 15 public void DisplayToConsole () 16 {17 Console. writeLine (this. instanceName); 18} 19} 20 21 public class Program22 {23 // use the delegate in the Main function to call this method 24 static void Main (string [] args) 25 {26 Test name = new Test ("Sirius"); 27 ShowValue method = name. displayToConsole; 28 method (); 29 Console. readKey (); 30} 31} 32}

Action is like this:

1 static void Main(string[] args)2 {3     Test name = new Test("Sirius");4     Action method = name.DisplayToConsole;5     method();6     Console.ReadKey();7 }

 

Of course, Action can also have a signature template, Action <T>.

public void DisplayToConsole(string name){      Console.WriteLine(name);}
Action<string> method2 = name.DisplayToConsole;method2("123");Console.ReadKey();

 

Speaking of Func, the difference with Action is that the Func proxy method must have a return value, because the Func overload mode is Func <T1, t2 .... TResult>, T represents the template of the proxy method, that is, the parameter that can be passed in, and TResult is the return value of the proxy method.

For example, we changed the DisplayToConsole method to DisplayForFunc:

1         public void DisplayForFunc(string name)2         {3             Console.WriteLine(name);4         }

This cannot be done when Func is used for delegation.

Func <string, object> f = t. DisplayForFunc;

The returned type error will be reported. The proxy used by Func should be of the return type. Let's modify the method and let it return a meaningless bool value:

1         public bool DisplayForFunc(string name)2         {3             Console.WriteLine(name);4             return true;5         }
1         static void Main(string[] args)2         {3             Test t = new Test("Sirius");4             Func<string, bool> f = t.DisplayForFunc;5             f("123");6             Console.ReadKey();            7         }

That's all!

 

By the way, I posted an awesome MSDN figure. This is the first time I saw MSDN and wanted to laugh ......

 

Code reference: https://msdn.microsoft.com/zh-cn/library/system.action (v = vs.110). aspx

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.