积极答复者
请教有无必要执行Dispose方法?

问题
答案
-
請參考。
http://msdn.microsoft.com/zh-tw/library/3cc9y48w(v=vs.80).aspx
我習慣利用using來讓object自己呼叫dispose,有關using可參考:
http://www.codeproject.com/Articles/6564/Understanding-the-using-statement-in-C
以上說明若有錯誤請指教,謝謝。
http://www.dotblogs.com.tw/terrychuang/- 已标记为答案 米斯瑞 2012年2月14日 7:36
-
对于SqlConnection、SqlCommand、SqlDataAdapter、SqlCommandBuilder、SqlTranscation这几个类的对象,是否操作完数据库都需要执行Dispose()?我知道SqlConnection是必须执行Close()或Dispose()的,那么其他不执行Dispose()可以吗?还是垃圾收集会自动处理的?最好官方给个明确的说法,谢谢!
补充:Dispose只是释放Connection中托管和非托管代码内部的一些空间,并不释放Connection本身。
namespaceA
{
classProgram
{
staticvoidMain(string[] args)
{
SqlConnectioncon=null;
using ( con=newSqlConnection("Server=.;database=northwind;integrated security=true"))
{
con.Open();
Console.WriteLine("OK");
con.Close();
}
Console.WriteLine(con==null); //False,证明尚未被释放,但是ConnectionString一类的属性已经被设置成空了。
}
}
}
QQ我:
下载MSDN桌面工具(Vista,Win7)
我的博客园
慈善点击,点击此处
- 已标记为答案 米斯瑞 2012年2月14日 7:36
- 已编辑 ThankfulHeart 2012年2月15日 5:18
-
Hi all,
如果要手动进行回收的话,就要在finalize里进行托管和非托管资源的回收,或者使用类似下面的代码:
class Test : IDisposable { public void Dispose() // NOT virtual { Dispose (true); GC.SuppressFinalize (this); // Prevent finalizer from running. } protected virtual void Dispose (bool disposing) { if (disposing) { // Call Dispose() on other objects owned by this instance. // You can reference other finalizable objects here. // ... } // Release unmanaged resources owned by (just) this object. // ... } ˜Test() { Dispose (false); } }
如果想看具体关于何时进行Dispose,可以看Oreilly出品的CSharp.4.0.in.a.Nutshell.4th,你可以在amazon中买到这本书:
http://www.amazon.com/C-4-0-Nutshell-Definitive-Reference/dp/0596800959
这本书同时更详细的介绍了如何进行重生以及自动垃圾回收机制的具体细节,其中的内容是基于最新的C# 4.0语言来阐述的。
orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
- 已标记为答案 米斯瑞 2012年2月14日 7:36
-
Hi all,
如果要手动进行回收的话,就要在finalize里进行托管和非托管资源的回收,或者使用类似下面的代码:
class Test : IDisposable { public void Dispose() // NOT virtual { Dispose (true); GC.SuppressFinalize (this); // Prevent finalizer from running. } protected virtual void Dispose (bool disposing) { if (disposing) { // Call Dispose() on other objects owned by this instance. // You can reference other finalizable objects here. // ... } // Release unmanaged resources owned by (just) this object. // ... } ˜Test() { Dispose (false); } }
如果想看具体关于何时进行Dispose,可以看Oreilly出品的CSharp.4.0.in.a.Nutshell.4th,你可以在amazon中买到这本书:
http://www.amazon.com/C-4-0-Nutshell-Definitive-Reference/dp/0596800959
这本书同时更详细的介绍了如何进行重生以及自动垃圾回收机制的具体细节,其中的内容是基于最新的C# 4.0语言来阐述的。
orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
呵呵!补充:)
似乎既然调用了
GC.SuppressFinalize (this);
其作用就是防止析构函数再次被调用吧?所以析构函数可以省略了。因为一旦disposing标识符是true,那么if内的托管代码和
if之外的非托管代码都被及时释放。
- 已标记为答案 米斯瑞 2012年2月14日 7:38
全部回复
-
請參考。
http://msdn.microsoft.com/zh-tw/library/3cc9y48w(v=vs.80).aspx
我習慣利用using來讓object自己呼叫dispose,有關using可參考:
http://www.codeproject.com/Articles/6564/Understanding-the-using-statement-in-C
以上說明若有錯誤請指教,謝謝。
http://www.dotblogs.com.tw/terrychuang/- 已标记为答案 米斯瑞 2012年2月14日 7:36
-
对于SqlConnection、SqlCommand、SqlDataAdapter、SqlCommandBuilder、SqlTranscation这几个类的对象,是否操作完数据库都需要执行Dispose()?我知道SqlConnection是必须执行Close()或Dispose()的,那么其他不执行Dispose()可以吗?还是垃圾收集会自动处理的?最好官方给个明确的说法,谢谢!
补充:Dispose只是释放Connection中托管和非托管代码内部的一些空间,并不释放Connection本身。
namespaceA
{
classProgram
{
staticvoidMain(string[] args)
{
SqlConnectioncon=null;
using ( con=newSqlConnection("Server=.;database=northwind;integrated security=true"))
{
con.Open();
Console.WriteLine("OK");
con.Close();
}
Console.WriteLine(con==null); //False,证明尚未被释放,但是ConnectionString一类的属性已经被设置成空了。
}
}
}
QQ我:
下载MSDN桌面工具(Vista,Win7)
我的博客园
慈善点击,点击此处
- 已标记为答案 米斯瑞 2012年2月14日 7:36
- 已编辑 ThankfulHeart 2012年2月15日 5:18
-
Hi all,
如果要手动进行回收的话,就要在finalize里进行托管和非托管资源的回收,或者使用类似下面的代码:
class Test : IDisposable { public void Dispose() // NOT virtual { Dispose (true); GC.SuppressFinalize (this); // Prevent finalizer from running. } protected virtual void Dispose (bool disposing) { if (disposing) { // Call Dispose() on other objects owned by this instance. // You can reference other finalizable objects here. // ... } // Release unmanaged resources owned by (just) this object. // ... } ˜Test() { Dispose (false); } }
如果想看具体关于何时进行Dispose,可以看Oreilly出品的CSharp.4.0.in.a.Nutshell.4th,你可以在amazon中买到这本书:
http://www.amazon.com/C-4-0-Nutshell-Definitive-Reference/dp/0596800959
这本书同时更详细的介绍了如何进行重生以及自动垃圾回收机制的具体细节,其中的内容是基于最新的C# 4.0语言来阐述的。
orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
- 已标记为答案 米斯瑞 2012年2月14日 7:36
-
Hi all,
如果要手动进行回收的话,就要在finalize里进行托管和非托管资源的回收,或者使用类似下面的代码:
class Test : IDisposable { public void Dispose() // NOT virtual { Dispose (true); GC.SuppressFinalize (this); // Prevent finalizer from running. } protected virtual void Dispose (bool disposing) { if (disposing) { // Call Dispose() on other objects owned by this instance. // You can reference other finalizable objects here. // ... } // Release unmanaged resources owned by (just) this object. // ... } ˜Test() { Dispose (false); } }
如果想看具体关于何时进行Dispose,可以看Oreilly出品的CSharp.4.0.in.a.Nutshell.4th,你可以在amazon中买到这本书:
http://www.amazon.com/C-4-0-Nutshell-Definitive-Reference/dp/0596800959
这本书同时更详细的介绍了如何进行重生以及自动垃圾回收机制的具体细节,其中的内容是基于最新的C# 4.0语言来阐述的。
orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
呵呵!补充:)
似乎既然调用了
GC.SuppressFinalize (this);
其作用就是防止析构函数再次被调用吧?所以析构函数可以省略了。因为一旦disposing标识符是true,那么if内的托管代码和
if之外的非托管代码都被及时释放。
- 已标记为答案 米斯瑞 2012年2月14日 7:38