C#2.0 新特性探究(二) 委托与匿名委托

C#2.0 新特性探究(二) 委托与匿名委托

delegate经常被人用来与回调相比较,其实两者在某种程度上由很多共同点。不过delegate有很多更加强大的地方。

首先,delegate中可以注册任意多个回调,在一个delegate被调用的时候,已经注册的过程将会被逐个调用。

其次,delegate允许注册一个对象的方法,而不像C++中指可以使用静态方法或者全局方法作为函数指针,提供了更多的灵活性,同时也暗示我们,delegate中按照某种方式保存了object的很多信息。

在C#2.0的匿名delegate中,我们甚至可以访问当前匿名委托的上下文变量。接下来的文章中我们将会通过实际的例子,来看看强大的delegate。

首先,让我们看看在C#1.2中的一个典型的委托的写法。

public delegate void TheEvent(int a);

public void test()

{

TheEvent testdel1 = new TheEvent(del1);

testdel1(12);

}

public void del1(int x)

{

Console.WriteLine("output x : {0}", x);

}

现在我们可以写成这样:

public void test()

{

TheEvent testdel1 = del1;

testdel1(12);

}

或者将程序改写为:

delegate void TheEvent2(int a);

public void test2()

{

int a = 12;

TheEvent ev2 = delegate(ref int x)

{ Console.WriteLine("output x : {0}", x); };

ev2( ref a);

}

比起1.2来,delegate的可读性更好,但是似乎没有本质的提高?慢着,让我们看看下面的例子。

public static void test3()

{

int a = 12;

int y = 32;

TheEvent ev2 = delegate(ref int x)

{ Console.WriteLine("output x + y : {0}", x + y); };

ev2( ref a);

}

注意,匿名函数中的内容!x + y的值被正确的输出了,而在1.2中,委托对于局部变量是没有除参数外的访问方式的。这样做有些什么好处呢?

让我们看一个更加复杂的例子:

public static void test4()

{

int a = 12;

int y = 32;

TheEvent ev2 = delegate(ref int x)

{ Console.WriteLine("output x + y : {0}", x + y); Thread.Sleep(100); };

//ev2(ref a);

IAsyncResult ar = ev2.BeginInvoke(ref a,

delegate(IAsyncResult ar2)

{Console.Write("Operation finished: {0} on thread ID:{1}, is pool: {2}",ar2.IsCompleted,Thread.CurrentThread.GetHashCode(), Thread.CurrentThread.IsThreadPoolThread);}

, null);

Console.WriteLine("do some other calculations while counter thread is working");

Console.Write("work status : {0} Main Thread ID:{1}, is pool: {2}",

ar.IsCompleted,

Thread.CurrentThread.GetHashCode(),

Thread.CurrentThread.IsThreadPoolThread);

Thread.Sleep(500);

ev2.EndInvoke(ref a, ar);

}

这个例子中使用了系统线程池对于任务进行排队,适合于IO或者计算密集型的操作的时候。使用匿名委托最大的好处在于可以完整地克隆当前运行空间上下文的可用变量,(虽然这可能从另一个层面上也增加了同步的复杂度,所谓有得必有失,hehe)。

[to be continued…]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

fanhgye

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

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

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

打赏作者

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

抵扣说明:

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

余额充值