using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
foo();
}
catch
{
Console.WriteLine("Exception Handler");
}
}
static void foo()
{
using (TestClass tc = new TestClass())
{
throw null;
}
}
} class TestClass : IDisposable
{
~TestClass() { Console.WriteLine("~TestClass"); } public void Dispose() { Console.WriteLine("Dispose"); }
}
}
输出什么为什么不是
Dispose
~TestClass
Exception Handler
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
foo();
}
catch
{
Console.WriteLine("Exception Handler");
}
}
static void foo()
{
using (TestClass tc = new TestClass())
{
throw null;
}
}
} class TestClass : IDisposable
{
~TestClass() { Console.WriteLine("~TestClass"); } public void Dispose() { Console.WriteLine("Dispose"); }
}
}
输出什么为什么不是
Dispose
~TestClass
Exception Handler
解决方案 »
- 关于linqtosql,我把数据库的表映射到实体,然后对实体进行操作,可是我又要改变数据库表(增加或者修改字段),请问是不是非得删掉重新建立一个实体?
- VS2005在Web.Config文件里怎么连接ACCESS数据库(急急!!!!!!!!!!)
- 为什么我自己写了一个用户控件,拖访到窗体里边,报错说“未将对象引用设置到对象的实例”
- 请帮看下这两句的区别
- 我想实现如下功能:将DataGrid中显示出来的数据中特定的几行设为只读,不能修改,该怎么做?
- net framework里3des加密问题,急,1000分求助(本人手头还有3000分)
- vs2008+sql2005的安装问题
- 如何在Web程序中调用系统剪贴板?
- 望高手解惑:C#里怎么把一串数字转换成16进制?
- 請教C#和Delphi6.0的高手.C#編譯的exe執行效率和Delphi6.0比較.哪個比較快.C#編譯的是IL碼. Dephi6.0是如何編譯的?
- 哪位好心的朋友,把我写的发送字符串的socket程序,改写成 发送一个很长的二进制数字的程序?基本结构不变
- 高手关注关于tabcontrol控件的用法?
接着 throw null;会被catch接住;程序退出是 析构函数执行,释放资源
Dispose
Exception Handler
~TestClass
为什么~TestClass是在最后的?
~TestClass() { Console.WriteLine("~TestClass"); }
这个东西是方法还是其他东西?
~TestClassfoo(); 进入后 实例化TestClass tc = new TestClass()
抛出异常 输出Exception Handler
析构函数 最后输出~TestClass
感觉 呵呵
析构函数,无法调用,程序退出是,释放资源,此时执行经典的例子就是 class A
{
public A()
{
Console.WriteLine(" A被创建");
}
~A()
{
Console.WriteLine(" A被释放");
}
} class B : A
{
public B()
{
Console.WriteLine("B被创建");
}
~B()
{
Console.WriteLine("B被释放");
} }
class C : B
{
public C()
{
Console.WriteLine("c被创建");
} ~C()
{
Console.WriteLine("B被释放");
}
}
class Program
{
static void Main(string[] args)
{
C c = new C();
Console.ReadLine(); }
}分别按F5和ctrl+F5看看结果,你就明白了
~TestClass
Dispose
Exception Handler
~TestClassusing实质:
在程序编译阶段,编译器会自动将using语句生成为try-finally语句,并在finally块中调用对象的Dispose方法,来清理资源。
所以using (TestClass tc = new TestClass())等同于:
try{
TestClass tc = new TestClass();
}catch(exception e){
}
finally{
((IDisposable)tc).Dispose();
}
所以,首先执行using (TestClass tc = new TestClass())这句
1. Using模块运行到结束时,TestClass的Dispose()将会被执行。
2. Exception被catch,所以执行Console.WriteLine("Exception Handler");
3. using{}后(tc对象只能在using{}内存活),对象tc需要被销毁,所以析构函数被调用。
2. (throw null)Exception被catch,所以执行Console.WriteLine("Exception Handler");
3. using{}后(tc对象只能在using{}内存活),对象tc需要被销毁,所以析构函数被调用。
此帖能加深理解
{
static void Main(string[] args)
{
try
{
foo();
}
catch
{
Console.WriteLine("Exception Handler");
}
}
static void foo()
{
using (TestClass tc = new TestClass())
{
throw null;
}
}
}class TestClass : IDisposable
{
~TestClass()
{
Console.WriteLine("~TestClass");
this.Dispose();
} public void Dispose()
{
Console.WriteLine("Dispose");
GC.SuppressFinalize(this);
}
}
Exception Handler
,析构函数不执行
Exception Handler
析构函数没执行
个人觉得不是using 结束调用Dispose(),而是using(TestClass tc = new TestClass())结束就调用Dispose(),而using(TestClass tc = new TestClass()){}结束后,{}这个是tc的存活范围,所以tc对象需要被销毁,所以此时系统会执行析构函数。
注意,我在vs2010下调试了,析构函数会被调用,如果有人说不会被调用,可能.net framework的版本不同吧
using{}后(tc对象只能在using{}内存活),对象tc需要被销毁,所以系统会执行析构函数。我在vs2010下调是过了,析构函数会被最后执行的,如果有人析构函数没有执行,那可能是.net framework的版本问题
接口的实例对象的话,那dispose()是一定会被调用的,应该和throw没关系吧。求其它高人详解