为什么释放对象上不脱手后使用它抛出的异常? [英] Why disposed object doesn't throw exception on using it after disposing?

查看:121
本文介绍了为什么释放对象上不脱手后使用它抛出的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是合法的处置调用对象的方法?如果是,为什么?

Is it legal to call a method on disposed object? If yes, why?

在下面的演示程序,我有一个一次性类 A (实现的IDisposable 接口)。就我所知,如果我通过使用()一次性对象 结构,那么的Dispose()方法被在右括号自动调用:

In the following demo program, I've a disposable class A (which implements IDisposable interface).As far as I know, if I pass disposable object to using() construct, then Dispose() method gets called automatically at the closing bracket:

A a = new A();
using (a)
{
   //...
}//<--------- a.Dispose() gets called here!

//here the object is supposed to be disposed, 
//and shouldn't be used, as far as I understand.

如果这是正确的,那么请你解释一下这个程序的输出:

If that is correct, then please explain the output of this program:

public class A : IDisposable
{
   int i = 100;
   public void Dispose()
   {
      Console.WriteLine("Dispose() called");
   }
   public void f()
   {
      Console.WriteLine("{0}", i); i  *= 2;
   }
}

public class Test
{
        public static void Main()
        {
                A a = new A();
                Console.WriteLine("Before using()");
                a.f();
                using ( a) 
                {
                    Console.WriteLine("Inside using()");
                    a.f();
                }
                Console.WriteLine("After using()");
                a.f();
        }
}

输出(的ideone ):

Before using()
100
Inside using()
200
Dispose() called
After using()
400

我怎么能叫 F()被处置对象 A ?这可以吗?如果是的话,为什么?如果没有,那么为什么上面的程序在运行时不给例外呢?

How can I call f() on the disposed object a? Is this allowed? If yes, then why? If no, then why the above program doesn't give exception at runtime?

我知道,使用流行的构建使用是这样的:

I know that the popular construct of using using is this:

using (A a = new A())
{
   //working with a
}

不过,我只是尝试,这就是为什么我写了不同的看法。

But I'm just experimenting, that is why I wrote it differently.

推荐答案

处置不等于没有了。仅设置意味着任何非托管资源(如文件,任何一种...的连接)已被释放。虽然这通常意味着对象不提供任何有用的功能,还有可能是不依赖于非托管资源,仍然照常工作方法。

Disposed doesn't mean gone. Disposed only means that any unmanaged resource (like a file, connection of any kind, ...) has been released. While this usually means that the object doesn't provide any useful functionality, there might still be methods that don't depend on that unmanaged resource and still work as usual.

该处置机制存在的.NET(和inheritly,C#.NET)是一个垃圾收集的环境中,这意味着你是不是RESPONSABLE进行内存管理。但是,如果非托管资源已经使用完的垃圾收集器无法确定,因此需要这个做自己。

The Disposing mechanism exist as .net (and inheritly, C#.net) is a garbage-collected environment, meaning you aren't responsable for memory management. However, the garbage collector can't decide if an unmanaged resource has been finished using, thus you need to do this yourself.

如果你想方法后,抛出一个异常该对象已diposed,你需要一个布尔捕捉处置状态,一旦该对象被设置,你抛出该异常:

If you want methods to throw an exception after the object has been diposed, you'll need a boolean to capture the dispose status, and once the object is disposed, you throw the exception:

public class A : IDisposable
{
   int i = 100;
   bool disposed = false;
   public void Dispose()
   {
      disposed = true;
      Console.WriteLine("Dispose() called");
   }
   public void f()
   {
      if(disposed)
        throw new ObjectDisposedException();

      Console.WriteLine("{0}", i); i  *= 2;
   }
}

这篇关于为什么释放对象上不脱手后使用它抛出的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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