当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > C#2.0 新特性探究之委托与匿名委托

ASP.NET
如何在ASP.NET中使用SmtpMail发送邮件
在VB.NET中利用Split和Replace函数计算字数
Attribute应用:简化ANF自定义控件初始化过程
ASP.NET 2.0移动开发入门之使用样式
ASP.NET 2.0中使用OWC生成图表
ASP.NET 2.0中控件的简单异步回调
一个无法捕获ADO.NET Dataset的内存错误
深入解读ADO.NET2.0的十大最新特性
.Net平台下的分布式缓存设计
ASP.NET全局异常处理浅析
ASP.NET 2.0中文验证码的实现
浅析.NET平台编程语言的未来走向
.net 框架程序设计收藏
使用ASP.NET MVC Futures 中的异步Action
详解.NET中的XmlReader与XmlWriter
关于.NET中的Server push技术
asp.net页面执行机制
对比JSP和ASP.NET的存储过程
.NET 4.0不会包含System.Shell.CommandLine
ASP.NET十个有效性能优化的方法

ASP.NET 中的 C#2.0 新特性探究之委托与匿名委托


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 73 ::
收藏到网摘: n/a

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或者计算密集型的操作的时候。使用匿名委托最大的好处在于可以完整地克隆当前运行空间上下文的可用变量,虽然这可能从另一个层面上也增加了同步的复杂度,所谓有得必有失。