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…]

Published At
Categories with Web编程
Tagged with
comments powered by Disqus