//判断今天星期几
//这个action方法根据c的int值判断,返回字符串
Func<int, string> action = (c) =>
 {
     switch (c)
     {
         case 1:
             return "週一";
         case 2:
             return "週二";
         case 3:
             return "週三";
         case 4:
             return "週四";
         case 5:
             return "週五";
         case 6:
             return "週六";
         default:
             return "週日";
     }
 };
 //调用,今天星期几
 string d03 = action(DateTime.Today.DayOfWeek.GetHashCode());
 

 //匿名方法
 //封装一个方法,该方法具有两个参数且不返回值。
 Action<string, string> action =  (a, b) =>
 {
     Console.WriteLine(a + " " + b);
 };
 //匿名異步方法
 //封装一个方法,该方法具有两个参数且不返回值。
 Action<string, string> action2 =async(a, b) =>
 {
     Console.WriteLine(a + " " + b);
     await Task.FromResult(12);
 };

Action line = () => Console.WriteLine();

//封装一个方法,该方法具有两个参数,并返回由 TResult 参数指定的类型的值。
//其中Func<int, int, bool>這裡的第一個int是指參數x的類型,
//第二個int是指參數y的類型,類型bool表示返回的數據類型
Func<int, int, bool> testEquality = (x, y) => x == y;
//調用方法
bool kmsg = testEquality(12, 5);

Func<int, string, bool> isTooLong = (int x, string s) => s.Length > x;

参考微软博文
https://docs.microsoft.com/zh-cn/dotnet/csharp/lambda-expressions


本文转载:CSDN博客