处置自定义类:设置 NULL VS .Dispose
可能的重复:
在类上调用 null 与 Dispose()
只是想要一些有关的信息处置对象。
我创建了一个从 IDISPOSIBLE 接口继承的 Employee 类。下面是示例代码
public class Employee : IDisposable
{
private Int32 _RunID;
public Int32 RunID { get { return _RunID; } set { _RunID = value; } }
public void Dispose()
{
//Dispose(true);
}
}
现在我的问题是,处理我们创建的每个类并使用 IDisposible 接口实现/继承它是一个很好的编码实践,即使我看到许多其他人直接设置 ObjEmployee = null; 的代码也是如此。所以只是很困惑,设置 NULL 好还是使用 IDisposible 接口实现它好还是以上都没有?
Possible Duplicate:
Calling null on a class vs Dispose()
Just want some information regarding Disposing an Object.
I have create a Employee Class which i inherit from IDISPOSIBLE Interface. Below is the sample Code
public class Employee : IDisposable
{
private Int32 _RunID;
public Int32 RunID { get { return _RunID; } set { _RunID = value; } }
public void Dispose()
{
//Dispose(true);
}
}
Now my Question is, it a Good Coding Practice to Dispose every class we create and implementing/Inheriting it with IDisposible Interface even i have seen many other people code they directly set ObjEmployee = null; so just got confused which is good setting NULL or Implementing it With IDisposible Interface or None Of the Above ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于,您是否有需要与对象一起删除的托管资源(文件句柄、套接字、连接等...)?如果是,那么如果您的类包含基本类型或仅包含不需要处理的信息,则需要 Dispose(),并且设置为 null 将提示 GC 清除该内存。
It depends, do you have managed resources (File handles, sockets, connections, etc....) that need to be gotten rid of along with your object? If yes then you need a Dispose() if your class contains basic types or just information you do not need to dispose and setting to null will give a hint to the GC to clear that memory.
当您设置 ObjEmployee = null 时,您仅将对象实例标记为已准备好接受垃圾收集器,但您无法影响实际清理的时间,并且可能需要一段时间。当您使用 Dispose() 方法时,GC 会立即运行并释放对象正在使用的内存。
When you set ObjEmployee = null, you only mark the object instance to be ready for the Garbage Collector, but you have no influence over when the actual clean-up would take place, and it may take a while. When you use the Dispose() method, the GC runs immediately and frees the memory the object was using.
在决定一个类是否应该实现 IDisposable 时,基本问题是该类的实例是否承担了清理其他实体的责任;通常,那些其他实体将代表 IDisposable 对象并以牺牲其他实体为代价来改变其行为,并且 IDisposable 对象负责让它们知道何时不再需要这样做。
例如,如果任何地方的任何代码使用 C 函数 fopen() 打开位于服务器上的文件以进行读写访问,则服务器将通过禁止其他任何人访问该文件来改变其行为,直到它收到单词打开它的程序不再需要它。当程序不再需要独占使用该文件时,它可以调用 fclose(),这将导致服务器收到通知该文件应再次可供其他应用程序使用。
如果 C# 类中的方法调用一个调用 fopen() 的例程,并且该例程在将 FILE* 放入 C# 程序知道的位置(但其他什么都不做)后返回,则该方法负责查看 fclose()必须以某种方式使用该 FILE* 进行调用。该文件需要 fclose(),并且系统中没有其他任何东西具有执行此操作所需的信息或动力,因此责任落在该 C# 类身上。
如果 C# 方法返回而不将 FILE* 存储在任何地方,则该文件将永远不会被关闭,并且宇宙中任何地方的其他人都无法使用它,除非或直到应用程序退出。如果 C# 方法必须在不产生文件独占使用的情况下退出,则它必须以某种方式存储 FILE*,以确保不再需要独占使用后某个地方的某人会清理它。正常模式是方法将 FILE* 存储在类字段中,并且对于包含通过复制和清空该字段来实现 IDisposable 的方法的类,查看它是否为非空,以及是否为非空,调用 fclose() 存储的 FILE*。
需要意识到的重要一点是,当垃圾收集器销毁对象时,系统不会关心任何对象字段中的内容。它甚至不会看他们。重要的是该对象是否有任何未履行的责任,以确保外部实体(甚至可能不在同一台机器上)在不再需要其服务时得到通知。
The fundamental question, in deciding whether a class should implement IDisposable, is whether instances of that class have taken on the responsibility of seeing that other entities get cleaned up; typically those other entities will be altering their behavior on behalf of the IDisposable object and at the expense of other entities, and the IDisposable object is responsible for letting them know when they no longer need to do so.
For example, if any code, anywhere, uses the C function fopen() to open for read-write access a file which is located on a server, the server will alter its behavior by forbidding anyone else from accessing the file until it receives word that the program that opened it has no more need of it. When the program no longer needs exclusive use of the file, it can call fclose() which will in turn cause the server to be notified that the file should again be available to other applications.
If a method in a C# class calls a routine which calls fopen(), and that routine returns after putting the FILE* in a place the C# program knows about but nothing else does, that method takes on a responsibility for seeing that fclose() must somehow get called with that FILE*. The file needs to be fclosed(), and nothing else in the system has the information or impetus necessary to do so, so the responsiblity falls to that C# class.
If the C# method were to return without storing the FILE* anywhere, the file would never get closed, and nobody else, anywhere in the universe, would be able to use it unless or until the application exits. If the C# method has to exit without yielding exclusive use of the file, it must store the FILE* in a way that will ensure that someone, somewhere, will clean it up after exclusive use is no longer required. The normal pattern would be for the method to store the FILE* in a class field, and for class containing the method to implement IDisposable by copying and blanking that field, seeing if it was non-blank, and if it was non-blank, calling fclose() the stored FILE*.
The important thing to realize is that when an object is destroyed by the garbage collector, the system won't care what's in any of the object fields. It won't even look at them. What matters is whether the object has any unfulfilled responsibilities for ensuring that outside entities, which may not even be on the same machine, are informed when their services are no longer needed.