什么是c#.net中的finalize和dispose方法 [英] what is finalize and dispose method in c#.net

查看:135
本文介绍了什么是c#.net中的finalize和dispose方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.net(C#)中的finalize和dispose方法是什么,它的用途和使用位置

what is finalize and dispose method in .net(C#) and what is the use and where we use

推荐答案

http://www.c-sharpcorner.com/UploadFile/Ashish1/dispose02152006095441AM/dispose.aspx [ http://www.devx.com/dotnet/Article/33167 [
http://www.c-sharpcorner.com/UploadFile/Ashish1/dispose02152006095441AM/dispose.aspx[^]
http://www.devx.com/dotnet/Article/33167[^]


像往常一样,您应该阅读文档并进行自己的研究.
最终确定 [处置 [
As usual you should be reading the documentation and doing your own research.
Finalize[^]
Dispose[^]


Finalize()
1.这与C#中的析构函数~Object()语法在C#中等效.
在VB.Net中,您可以通过覆盖实现Finalize().但是,在C#中,编译器将析构函数转换为Finalize()方法.
2. Finalize()不能在C#中覆盖或调用.
3.因为Finalize()垃圾收集器调用,所以它是不确定的.

Dispose()
1.这必须在实现IDispose interface的类中实现. 2.它是释放非托管资源(例如文件,句柄和连接等)的正确位置.
3. Dispose()方法在代码本身中被显式调用.
4.在"using"语句中使用时,Dispose()方法将自动调用(对于实现IDispose的对象).
来自:
此处 [完成并处理 [ ^ ]

区别:
处置与最终确定 [ .Net中的Finalize()和Dispose()方法? [ ^ ]
Finalize()
1.This is the C# equivalent of destructor ~Object() syntax in C#.
In VB.Net you implement the Finalize() by overriding it. But, in C# the compiler translates the destructor to a Finalize() method.
2. Finalize() can NOT be overridden or called in C#.
3. Since, Finalize() is called by the Garbage Collector, it is non-deterministic.

Dispose()
1. This has to be implemented in classes implementing IDispose interface.
2. Its the right place for freeing-up unmanaged resources like file, handles, and connections etc.
3. Dispose() method is called explicitely in the code itself.
4. Dispose() method is automatically called (for objects which implement IDispose), when used in a "using" statement.
From: here[^]

Refer more detailed description with example on MSDN: Finalize and Dispose[^]

Difference:
Dispose Vs Finalize[^]
Finalize() and Dispose() methods in .Net?[^]


这篇关于什么是c#.net中的finalize和dispose方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆