C#匿名委托方法

//判断今天星期几
//这个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;



//方法签名:
public DispatcherOperation BeginInvoke(Delegate method, params object[] args);

//进度条更新
jindu.Dispatcher.BeginInvoke(new Action<int>((a) =>
	{
	   this.jindu.Value = a * 10;
	   this.jindu_value.Content = a * 10 + "%";
	})
, i); 
                    

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王焜棟琦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值