C# 匿名委托

C# 2.0中引入了匿名方法,但在C#3.0及更高版本中,Lambda表达式代替了匿名方法。

不过,本主题中有关匿名方法的信息同样也适用于 Lambda 表达式。 有一种情况下,匿名方法提供了 Lambda 表达式中所没有的功能。 您可使用匿名方法来忽略参数列表。 这意味着匿名方法可转换为具有各种签名的委托。 这对于 Lambda 表达式来说是不可能的。

/*************************************************案例1*******************************************/              

<span style="font-size:10px;">   this.Invoke(new EventHandler(</span>
<span style="font-size:10px;"><span style="white-space:pre">		</span>delegate{
                          //  XXXXXXXX;
                        })<span style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">);</span></span>
<span style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;"><span style="font-size:10px;">
</span></span>
<span style="font-family:Microsoft YaHei UI, Microsoft YaHei, SimSun, Segoe UI, Lucida Grande, Verdana, Arial, Helvetica, sans-serif;font-size:10px;">/***********************************************************案例2********************************************************/</span>
<span style="font-family:Microsoft YaHei UI, Microsoft YaHei, SimSun, Segoe UI, Lucida Grande, Verdana, Arial, Helvetica, sans-serif;"></span><pre style="padding: 5px; margin-top: 0px; margin-bottom: 0px; overflow: auto; word-wrap: normal; line-height: 17.549999237060547px; font-family: Consolas, Courier, monospace !important;"><span style="font-size:10px;">System.Threading.Thread t1 = <span style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; color: blue;">new</span> System.Threading.Thread(new ThreadStart(<span style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; color: blue;">delegate</span>()
            {
               // XXXXXXXX;
            }));
    t1.Start();</span>

 Lambda表达式的话 
 
<span style="font-family:Microsoft YaHei UI, Microsoft YaHei, SimSun, Segoe UI, Lucida Grande, Verdana, Arial, Helvetica, sans-serif;"></span><pre style="margin-top: 0px; margin-bottom: 0px; color: rgb(42, 42, 42); padding: 5px; overflow: auto; word-wrap: normal; line-height: 17.549999237060547px; font-family: Consolas, Courier, monospace !important;"><span style="font-size:10px;">System.Threading.Thread t1 = <span style="color: blue; font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;">new</span> System.Threading.Thread(new ThreadStart(()=>
            {
               // XXXXXXXX;
            }));
    t1.Start();</span>
/*************************************************************案例3*********************************************************/
 
<span style="font-family:Microsoft YaHei UI, Microsoft YaHei, SimSun, Segoe UI, Lucida Grande, Verdana, Arial, Helvetica, sans-serif;">using System;
using System.Collections.Generic;
using System.Text;


namespace ConsoleApplication3
{
    public delegate bool MYDEL();


    public  class Program
    {
        public  MYDEL mydel = null;
        static void Main(string[] args)
        {


            if (new Program() { mydel = new MYDEL(delegate() { Console.Write("HELLO! "); return false; }) }.mydel())</span>
<span style="font-family:Microsoft YaHei UI, Microsoft YaHei, SimSun, Segoe UI, Lucida Grande, Verdana, Arial, Helvetica, sans-serif;"><span style="white-space:pre">	</span>   //</span><span style="font-family: 'Courier New'; white-space: pre-wrap; color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span><span style="font-family: 'Courier New'; white-space: pre-wrap;"> (</span><span style="font-family: 'Courier New'; white-space: pre-wrap; color: rgb(0, 0, 255); line-height: 1.5 !important;">new</span><span style="font-family: 'Courier New'; white-space: pre-wrap;"> Func<</span><span style="font-family: 'Courier New'; white-space: pre-wrap; color: rgb(0, 0, 255); line-height: 1.5 !important;">bool</span><span style="font-family: 'Courier New'; white-space: pre-wrap;">>(() => { Console.Write(</span><span style="font-family: 'Courier New'; white-space: pre-wrap; color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="font-family: 'Courier New'; white-space: pre-wrap; color: rgb(128, 0, 0); line-height: 1.5 !important;">Hello </span><span style="font-family: 'Courier New'; white-space: pre-wrap; color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="font-family: 'Courier New'; white-space: pre-wrap;">); </span><span style="font-family: 'Courier New'; white-space: pre-wrap; color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span><span style="font-family: 'Courier New'; white-space: pre-wrap;"> </span><span style="font-family: 'Courier New'; white-space: pre-wrap; color: rgb(0, 0, 255); line-height: 1.5 !important;">false</span><span style="font-family: 'Courier New'; white-space: pre-wrap;">; }).Invoke())</span><span style="font-family:Microsoft YaHei UI, Microsoft YaHei, SimSun, Segoe UI, Lucida Grande, Verdana, Arial, Helvetica, sans-serif;">

            {
                Console.Write("HELLO!");
            }
            else
            {
                Console.Write("WORLD!");
                Console.Read();
            }
        }
    }
}
</span>

匿名委托关键字还是delegate

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值