gxh973121

博客园 首页 新随笔 联系 订阅 管理

比如下面代码:

using System;
class Class1
{
 [STAThread]
 static void Main(string[] args)
 {
  int m = add(1,2);
  Console.WriteLine(m);
  Console.ReadLine();
 }
 private static int add(int i,int j)
 {
  try
  {
   Console.WriteLine("haha1");
   return i + j;
   Console.WriteLine("haha2");
  }
  finally
  {
   Console.WriteLine("haha3");
  }
 }
}

执行的结果是:
haha1
haha3
3

这样如果我们一个函数中,有多个地方都要返回,我们就可以统一把资源的释放放在finally中完成。而不用再每一个return前都释放。

posted on 2005-02-25 09:19  gxh973121  阅读(509)  评论(0)    收藏  举报