澄清有关IDisposable接口的一些事情.调用Dispose之后,instance(必须为)是否等于null? [英] Clarify some things about IDisposable interface. Is instance (must be) equals null after calling Dispose?

查看:34
本文介绍了澄清有关IDisposable接口的一些事情.调用Dispose之后,instance(必须为)是否等于null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现IDisposable接口的类.

I have a class which implements IDisposable interface.

using System;

class A : IDisposable
{
        public void Dispose()
        {
            Stop(); // some actions to stop internal threads

            this = null;
        }
}

为什么不能在Dispose方法中分配 this = null ?我知道这"是只读的.

Why can't I assign this = null in Dispose method ? I know 'this' is read-only.

例如:

A a = new A();
a.Run();
// ...
a.Dispose();
// i want in this line a = null

我认为IDisposable和Dispose方法可以保证在调用Dispose()之后,类A的实例等于null.但这不是真的.

I thought IDisposable and Dispose method guarantee that instance of class A will be equals to null after calling Dispose(). But it's not true.

推荐答案

只需添加到已经说过的内容中即可:

Just to add to what's already been said:

重要的是要意识到有对象,并且有引用(变量),并且这些不是同一件事.

It's important to realize that there are objects, and there are references (variables), and these are not the same thing.

当您编写 var a = new object(); 时,您正在做两件事:

When you write var a = new object(); you're doing two things:

  1. 创建一个新的 object 实例.该实例没有名称"-只是内存中的一个位置.就是这样.但是,正好我们有个称呼,我们称它为鲍勃".
  2. 声明一个变量 a ,其中引用刚创建的对象.
  1. Creating a new object instance. This instance has no "name" -- just a location in memory. It is what it is. But, just so we have something to call it, let's call it "Bob."
  2. Declaring a variable a, which references the object just created.

现在,当您编写 a = null; 时,您对Bob所做的一切都不成立.您正在将 a 更改为引用,而不是Bob, null 或换句话说为无对象".因此,您不再可以使用 a 访问"Bob".

Now, when you write a = null;, you're doing nothing to Bob. You're changing a to reference, instead of Bob, null, or in other words, "no object." So you can no longer access "Bob" using a.

这是否意味着鲍勃现在不存在?否.鲍勃仍然在原地.

Does this mean now Bob doesn't exist? No. Bob's still right where he was.

考虑一下(基本上是

Consider this (which is basically the scenario Henk mentioned):

var a = new object(); // Again, we're calling this object Bob.
object b = a;

现在, b 是另一个变量,就像 a 一样.就像 a 一样, b 引用Bob.但是同样,就像 a 一样,写 b = null; 对Bob毫无帮助.它只是更改 b ,以使其不再指向对象.

Now, b is another variable, just like a. And just like a, b references Bob. But again, just as with a, writing b = null; does nothing to Bob. It just changes b so that it no longer points to an object.

这就是我要去的地方.您似乎一直对这样做有印象:

Here's where I'm going with this. You seem to have been under the impression that doing this:

a.Dispose();

...以某种方式也这样做:

...somehow also did this:

a = null;

...这在某种意义上等同于执行此操作:

...which was somehow equivalent to doing this:

Bob = null;//现在 Bob 没有对象吗?

但是,如果是这种情况,那么即使没有设置为 null, b (上方)现在也将指向 no object .

But if that were the case, then b (above) would now point to no object, even though it was never set to null!

无论如何,希望从我上面的解释中可以清楚地看出,这根本不是CLR的工作方式.正如乔恩指出的那样, IDisposable 接口实际上与释放的内存无关.它与释放共享资源有关.它不会-不能-从内存中删除对象,就像这样做了一样,我们将具有我上面描述的行为(引用突然从各处变得无效).

Anyway, from my explanation above, hopefully it is clear that this is simply not how the CLR works. As Jon has pointed out, the IDisposable interface is actually not related to memory being freed. It is about releasing shared resources. It does not -- cannot -- delete objects from memory, as if it did then we would have the behavior I've described above (references suddenly becoming invalid out of nowhere).

我知道这只是与您有关 IDisposable 的特定问题松散相关,但是我感觉到这个问题来自对变量和对象之间关系的误解.所以我想使这种关系更加清晰.

I know this was only loosely related to your specific question about IDisposable, but I sensed that this question was coming from a misconception about the relationship between variables and objects; and so I wanted to make that relationship clearer.

这篇关于澄清有关IDisposable接口的一些事情.调用Dispose之后,instance(必须为)是否等于null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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