我怎么知道什么时候需要处理一个物体?

时间:2023-02-09 15:47:30

HOW do i know when i need to dispose of something? Someone just mention i had several objects in my code that i need to dispose of. I had no idea i needed to dispose anything (this is my first week with C#). How do i know when i need to dispose an object? i was using http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.aspx and i do not see any mention of dispose on the page or seen it mention in any other objs i was told i to dispose (by someone on SO).

我怎么知道何时需要处理某些东西?有人提到我在代码中有几个需要处理的对象。我不知道我需要处理任何东西(这是我用C#的第一周)。我怎么知道什么时候需要处理一个物体?我正在使用http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.aspx,我没有看到任何提及页面上的处理或看到它提到任何其他objs我是告诉我要处理(由某人处理)。

I know i need to when something inherits IDisposable but HOW do i KNOW when it does inherit it?

我知道我需要什么东西继承IDisposable但我怎么知道什么时候它继承它?

8 个解决方案

#1


You should dispose anything that implements IDisposable. Just wrap it on an using:

你应该处置任何实现IDisposable的东西。只需将其包装在使用上:

   using(var some = new Something())
   {
    //use normally
   }

#2


Similar questions here:

类似的问题:

#3


An easy way would be to type obj.disp and see if intellisense has a dispose method.

一种简单的方法是键入obj.disp并查看intellisense是否具有dispose方法。

#4


The class implements the interface IDisposable, that means that it has a Dispose method.

该类实现了接口IDisposable,这意味着它具有Dispose方法。

Not every class that implements IDisposable requires you to call Dispose, but most of them do. If you see that the class implements IDisposable (or has a Dispose method because it inherits the interface from a base class), you have two choises:

并非每个实现IDisposable的类都要求您调用Dispose,但大多数都这样做。如果您看到该类实现了IDisposable(或者具有Dispose方法,因为它从基类继承了接口),您有两个选择:

  1. Dig deep in the documentation to find out why the class implements IDisposable, and if you really need to call Dispose.

    深入研究文档,找出该类实现IDisposable的原因,以及是否真的需要调用Dispose。

  2. Just call Dispose.

    只需调用Dispose。

Either method is safe. If the Dispose method doesn't do anything, the call will be very quick. You can even call Dispose more than once without harm.

这两种方法都是安全的。如果Dispose方法没有做任何事情,那么调用将非常快。您甚至可以多次调用Dispose而不会造成伤害。

Even better then just calling the Dispose method is to use a using block:

更好的是,只需调用Dispose方法就是使用using块:

using (FileStream s = File.OpenRead(path)) {
   ...
}

At the end bracket of the block the Dispose method is called automatically. The using block is implemented as a try...finally, so the Dispose method is guaranteed to be called even if an exception occurs in the block.

在块的结尾处,自动调用Dispose方法。 using块实现为try ... finally,因此即使块中发生异常,也可以保证调用Dispose方法。

#5


If an class implements IDisposable you should dispose of intances of that class. If it doesn't you don't. In this case HashAlgorithm derives from ICryptoTransform which derives from IDisposable. This means all instance of classes descending from HashAlgorithm must be disposed.

如果一个类实现了IDisposable,你应该处理该类的intances。如果不是你没有。在这种情况下,HashAlgorithm派生自ICryptoTransform,它派生自IDisposable。这意味着必须处理从HashAlgorithm下降的所有类实例。

#6


You should dispose of any object that implements the IDisposable interface.

您应该处置实现IDisposable接口的任何对象。

public abstract class HashAlgorithm : ICryptoTransform, 
IDisposable

Anything that has unmanaged resources (DB connections for example) should implement the IDisposable interface.

任何具有非托管资源(例如DB连接)的东西都应该实现IDisposable接口。

There are a couple of good reasons for this:

这有几个很好的理由:

  • You know that the unmanaged resources (which are typically quite scarce) are going to be cleaned up. Usually these will be cleared up in the finalizer anyway, but due to how the GC has to tidy up objects with finalizers this could take a while.
  • 您知道非托管资源(通常非常稀缺)将被清除。通常这些将在终结器中清除,但由于GC必须用终结器整理对象,这可能需要一段时间。

  • If you implement the standard dispose pattern you save the GC a lot of work as it doesn't need to call the finalizer.
  • 如果您实施标准配置模式,则可以节省GC大量工作,因为它不需要调用终结器。

#7


I know i need to when something inherits IDisposable but HOW do i KNOW when it does inherit it?

我知道我需要什么东西继承IDisposable但我怎么知道什么时候它继承它?

Assuming you're using Visual Studio. I usually right click on the type, then "Go To Definition". If I see that it, or any of its super classes, implement IDisposable, I make sure I call Dispose on it. This is typically done by wrapping it in a using block as others have mentioned.

假设您正在使用Visual Studio。我通常右键单击类型,然后“转到定义”。如果我看到它或它的任何超级类,实现IDisposable,我确保我在它上面调用Dispose。这通常通过将其包装在使用块中来完成,如其他人所提到的。

#8


"Will the last person to leave the room please turn out the lights?"

“最后一个人会离开房间,请把灯关掉吗?”

An object which implements IDisposable holds the information and impetus necessary to do some "clean-up" operations that should happen "sometime", but which can't happen while the object is still in use. If the object is totally abandoned, those clean-up operations won't happen. The system includes a custodian, with which objects can register when they are created; if an object is abandoned by absolutely everyone but the custodian, the custodian can ask the object to perform its cleanup actions before the custodian too abandons it. Note that for a variety of reasons, the custodian isn't 100% effective at handling abandoned objects. It is thus very desirable that, whenever possible, the last entity to hold a useful reference to an object dispose of it before abandoning the reference.

实现IDisposable的对象保存了执行某些“清理”操作所必需的信息和动力,这些操作应该“在某个时间”发生,但在对象仍在使用时不会发生。如果对象完全被放弃,那么这些清理操作就不会发生。该系统包括一个保管人,物品在创建时可以登记;如果一个物体被除了保管人以外的绝对所有人抛弃,保管人可以要求物体在保管人放弃之前执行其清理行动。请注意,由于各种原因,托管人在处理废弃物体方面不是100%有效。因此非常希望,只要有可能,在放弃引用之前,保持对对象的有用引用的最后一个实体处理它。

#1


You should dispose anything that implements IDisposable. Just wrap it on an using:

你应该处置任何实现IDisposable的东西。只需将其包装在使用上:

   using(var some = new Something())
   {
    //use normally
   }

#2


Similar questions here:

类似的问题:

#3


An easy way would be to type obj.disp and see if intellisense has a dispose method.

一种简单的方法是键入obj.disp并查看intellisense是否具有dispose方法。

#4


The class implements the interface IDisposable, that means that it has a Dispose method.

该类实现了接口IDisposable,这意味着它具有Dispose方法。

Not every class that implements IDisposable requires you to call Dispose, but most of them do. If you see that the class implements IDisposable (or has a Dispose method because it inherits the interface from a base class), you have two choises:

并非每个实现IDisposable的类都要求您调用Dispose,但大多数都这样做。如果您看到该类实现了IDisposable(或者具有Dispose方法,因为它从基类继承了接口),您有两个选择:

  1. Dig deep in the documentation to find out why the class implements IDisposable, and if you really need to call Dispose.

    深入研究文档,找出该类实现IDisposable的原因,以及是否真的需要调用Dispose。

  2. Just call Dispose.

    只需调用Dispose。

Either method is safe. If the Dispose method doesn't do anything, the call will be very quick. You can even call Dispose more than once without harm.

这两种方法都是安全的。如果Dispose方法没有做任何事情,那么调用将非常快。您甚至可以多次调用Dispose而不会造成伤害。

Even better then just calling the Dispose method is to use a using block:

更好的是,只需调用Dispose方法就是使用using块:

using (FileStream s = File.OpenRead(path)) {
   ...
}

At the end bracket of the block the Dispose method is called automatically. The using block is implemented as a try...finally, so the Dispose method is guaranteed to be called even if an exception occurs in the block.

在块的结尾处,自动调用Dispose方法。 using块实现为try ... finally,因此即使块中发生异常,也可以保证调用Dispose方法。

#5


If an class implements IDisposable you should dispose of intances of that class. If it doesn't you don't. In this case HashAlgorithm derives from ICryptoTransform which derives from IDisposable. This means all instance of classes descending from HashAlgorithm must be disposed.

如果一个类实现了IDisposable,你应该处理该类的intances。如果不是你没有。在这种情况下,HashAlgorithm派生自ICryptoTransform,它派生自IDisposable。这意味着必须处理从HashAlgorithm下降的所有类实例。

#6


You should dispose of any object that implements the IDisposable interface.

您应该处置实现IDisposable接口的任何对象。

public abstract class HashAlgorithm : ICryptoTransform, 
IDisposable

Anything that has unmanaged resources (DB connections for example) should implement the IDisposable interface.

任何具有非托管资源(例如DB连接)的东西都应该实现IDisposable接口。

There are a couple of good reasons for this:

这有几个很好的理由:

  • You know that the unmanaged resources (which are typically quite scarce) are going to be cleaned up. Usually these will be cleared up in the finalizer anyway, but due to how the GC has to tidy up objects with finalizers this could take a while.
  • 您知道非托管资源(通常非常稀缺)将被清除。通常这些将在终结器中清除,但由于GC必须用终结器整理对象,这可能需要一段时间。

  • If you implement the standard dispose pattern you save the GC a lot of work as it doesn't need to call the finalizer.
  • 如果您实施标准配置模式,则可以节省GC大量工作,因为它不需要调用终结器。

#7


I know i need to when something inherits IDisposable but HOW do i KNOW when it does inherit it?

我知道我需要什么东西继承IDisposable但我怎么知道什么时候它继承它?

Assuming you're using Visual Studio. I usually right click on the type, then "Go To Definition". If I see that it, or any of its super classes, implement IDisposable, I make sure I call Dispose on it. This is typically done by wrapping it in a using block as others have mentioned.

假设您正在使用Visual Studio。我通常右键单击类型,然后“转到定义”。如果我看到它或它的任何超级类,实现IDisposable,我确保我在它上面调用Dispose。这通常通过将其包装在使用块中来完成,如其他人所提到的。

#8


"Will the last person to leave the room please turn out the lights?"

“最后一个人会离开房间,请把灯关掉吗?”

An object which implements IDisposable holds the information and impetus necessary to do some "clean-up" operations that should happen "sometime", but which can't happen while the object is still in use. If the object is totally abandoned, those clean-up operations won't happen. The system includes a custodian, with which objects can register when they are created; if an object is abandoned by absolutely everyone but the custodian, the custodian can ask the object to perform its cleanup actions before the custodian too abandons it. Note that for a variety of reasons, the custodian isn't 100% effective at handling abandoned objects. It is thus very desirable that, whenever possible, the last entity to hold a useful reference to an object dispose of it before abandoning the reference.

实现IDisposable的对象保存了执行某些“清理”操作所必需的信息和动力,这些操作应该“在某个时间”发生,但在对象仍在使用时不会发生。如果对象完全被放弃,那么这些清理操作就不会发生。该系统包括一个保管人,物品在创建时可以登记;如果一个物体被除了保管人以外的绝对所有人抛弃,保管人可以要求物体在保管人放弃之前执行其清理行动。请注意,由于各种原因,托管人在处理废弃物体方面不是100%有效。因此非常希望,只要有可能,在放弃引用之前,保持对对象的有用引用的最后一个实体处理它。