object.close()或object = null? [英] object.close() or object = null?

查看:123
本文介绍了object.close()或object = null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以即时通讯试图保持良好状态并且不要让任何东西悬空但我想我已经看到了各种各样的方法来杀死物品......有没有像推荐的方式那样?


基本上我正在寻找处理对象破坏的最佳实践。


谢谢

贾斯汀

解决方案

Justin,


如果对象实例的类型实现了IDisposable,那么你应该调用Dispose方法那个接口实现。

使用构造很方便:


使用(MyObject myObject = new MyObject())

{

//在这里使用myObject 。

}


退出范围时(由于返回值,异常或

代码退出范围) )将调用Dispose方法

MyObject'的IDisposable实现。


如果某个类型没有实现IDisposable,那么你没有必要做任何事情的对象,CLR最终将通过

GC来处理它。


-

- Nicholas Paldino [.NET / C#MVP]

- mv *@spam.guard.caspershouse.com


" Justin Rich" < jr ****** @ yahoo.spam.com写信息

新闻:u7 ************** @ TK2MSFTNGP04.phx.gbl .. 。


所以我试图保持良好状态并且不要让任何东西悬空但我想

我已经看到了各种杀死物体的方法。有没有推荐的

方式?


基本上我正在寻找处理对象破坏的最佳实践。


谢谢

贾斯汀



Justin Rich写道:


所以即时通讯试图保持良好状态并且不要让任何东西悬空但我想我已经看到了各种杀死物品的方法......有没有像推荐的方式那样?


基本上我正在寻找处理对象破坏的最佳实践。



像大多数事情一样,没有一个尺码适合所有答案。如果对象

是一个连接到资源的对象,例如SQL数据库,则

它可能有一个应该调用的close方法。如果

中的对象实现了IDisposable,那么你想确保通过创建

来显式或隐式调用它的

Dispose方法具有使用的对象关键词。如果对象只是一个用于

非托管COM对象的RTW,那么你可能需要正确释放底层

来对象,可能需要调用Marshal.ReleaseComObject 。我已经看到将对象设置为null以获得好处是很少见的。


但答案取决于对象是什么,

对象的范围,对象所需的生命周期等。

-

Tom Porterfield




" Tom Porterfield" < tp ****** @ mvps.org写信息

新闻:OH **************** @ TK2MSFTNGP02.phx.gbl .. 。


Justin Rich写道:


>所以我试图保持良好状态,不要留下任何悬挂打开,但我想我已经看到了各种方法来杀死对象..是否有像推荐的方式?

基本上我正在寻找处理对象破坏的最佳实践。



像大多数事情一样,没有一个尺寸适合所有答案。如果对象是连接到资源(例如SQL数据库)的对象,那么它可能有一个应该调用的close方法。如果

层次结构中的对象实现了IDisposable,那么你想确保通过创建对象显式或隐式调用它的Dispose

方法

使用使用关键词。如果对象只是一个非托管的
COM对象的RTW,那么你可能需要正确释放底层的

对象,可能需要调用Marshal.ReleaseComObject 。

我很少看到将对象设置为null以获得好处。



无论实际的清理操作是Close,Flush,ReleaseComObject还是

,几乎每个对象都提供一个Dispose方法。正确的

的事情。如果它实现了IDisposable,请调用Dispose。任何其他清理

要求应被视为一个错误。


唯一使用variable = null;当你处于

线程的主循环中时,你的函数在很长一段时间内都不会关闭。或者如果

变量是类成员。特别是类的静态成员将始终可以访问
,因此GC将它们视为正在使用中,并且从不清除它们,因此在这种情况下您应该将其清空。


>

但答案取决于对象是什么,

的范围对象,对象所需的生命周期等。

-

Tom Porterfield


so im trying to be good and not leave anything hanging open but i guess ive
seen a variety of ways to kill objects.. is there like a recommended way?

basically im looking for best practices for handling object destruction.

Thanks
Justin

解决方案

Justin,

If the type of the object instance implements IDisposable, then you
should call the Dispose method on that interface implementation. The
"using" construct is handy for that:

using (MyObject myObject = new MyObject())
{
// Use myObject here.
}

When the scope is exited (because of return value, exception, or the
code just exits the scope) the Dispose method is going to be called on
MyObject''s implementation of IDisposable.

If a type doesn''t implement IDisposable, then you don''t have to do
anything with the object, the CLR will take care of it eventually through
GC.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Justin Rich" <jr******@yahoo.spam.comwrote in message
news:u7**************@TK2MSFTNGP04.phx.gbl...

so im trying to be good and not leave anything hanging open but i guess
ive seen a variety of ways to kill objects.. is there like a recommended
way?

basically im looking for best practices for handling object destruction.

Thanks
Justin



Justin Rich wrote:

so im trying to be good and not leave anything hanging open but i guess ive
seen a variety of ways to kill objects.. is there like a recommended way?

basically im looking for best practices for handling object destruction.

Like most things, there is no one size fits all answer. If the object
is an object that connects to a resource, such as a SQL database, then
it likely has a close method that should be called. If the object in
its hierarchy implements IDisposable then you want to make sure its
Dispose method is called either explicitly or implicitly by creating the
object with the "using" keyword. If the object is just an RTW for an
unmanaged COM object then you''ll want to properly release the underlying
come object, possibly with a call to Marshal.ReleaseComObject. It is
rare that I''ve seen setting the object to null to have a benefit.

But the answer varies depending on what the object is, the scope of the
object, the needed lifetime of the object, etc.
--
Tom Porterfield



"Tom Porterfield" <tp******@mvps.orgwrote in message
news:OH****************@TK2MSFTNGP02.phx.gbl...

Justin Rich wrote:

>so im trying to be good and not leave anything hanging open but i guess
ive seen a variety of ways to kill objects.. is there like a recommended
way?

basically im looking for best practices for handling object destruction.


Like most things, there is no one size fits all answer. If the object is
an object that connects to a resource, such as a SQL database, then it
likely has a close method that should be called. If the object in its
hierarchy implements IDisposable then you want to make sure its Dispose
method is called either explicitly or implicitly by creating the object
with the "using" keyword. If the object is just an RTW for an unmanaged
COM object then you''ll want to properly release the underlying come
object, possibly with a call to Marshal.ReleaseComObject. It is rare that
I''ve seen setting the object to null to have a benefit.

Whether the actual cleanup action is Close, Flush, ReleaseComObject, or
whatever, nearly every object provides a Dispose method that "does the right
thing". If it implements IDisposable, call Dispose. Any other cleanup
requirement should be considered a bug.

The only use of "variable = null;" is when you''re in the main loop of the
thread, where your function won''t close for a very long time. Or if the
variable is a class member. Especially static members of classes will
always be reachable so the GC sees them as being in-use and never cleans
them up, so in that case you should null it out.

>
But the answer varies depending on what the object is, the scope of the
object, the needed lifetime of the object, etc.
--
Tom Porterfield



这篇关于object.close()或object = null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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