.net中finalize和finalizer有什么区别? [英] what is the difference between finalize and finalizer in .net ?

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

问题描述

.net中finalize和finalizer的区别是什么?

finalize和finalizer有什么不同吗?

什么是终结器?

what is the difference between finalize and finalizer in .net ?
Is there any difference in finalize and finalizer?
What is finalizer?

推荐答案

敲定方法 [ ^ ]用于允许您自己清理并释放您的类实例创建的任何资源。



A < a href =http://en.wikipedia.org/wiki/Finalizer>终结者 [ ^ ]只是这些方法的通用术语。
The Finalize method[^] is used to allow you to "clean up" after yourself and release any resources your class instance has created.

A Finalizer[^] is just the generic term for such methods.


Finalizer是类的析构函数。 Finalize是垃圾收集器调用的方法。我想我记得Finalize是由析构函数隐式调用的,但是我没有检查我现在是否记得正确:)



类似,但不一样的是Dispose方法实现IDisposable的对象使用的。好的做法是说你应该在实现IDisposable的对象上调用Dispose。你永远不应该直接调用Finalize(或析构函数)。



您可以使用GC.Collect()强制进行垃圾回收,但这可能会导致性能问题,不建议。在需要的地方调用Dispose,让GC发挥其魔力。
Finalizer is destructor of the class. Finalize is the method called by the garbage collector. I think I remember Finalize is implicitly called by the destructor, but I didn't check if I remember correctly right now :)

Similar, but not same is Dispose method that is used by objects implementing IDisposable. Good practice says you should call Dispose on the object implementing IDisposable. You should NEVER call Finalize directly (or destructor for that matter).

You can force garbage collection with GC.Collect(), but this can have performance issues and is not recommended. Call Dispose where needed and let GC do its magic.


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

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