Why disposed object doesn't throw exception on using it after disposing?

前端 未结 6 1942
太阳男子
太阳男子 2020-12-11 15:11

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

In the following demo program, I\'ve a disposable class A (which implements IDisposa

6条回答
  •  悲&欢浪女
    2020-12-11 15:55

    A typical Dispose() implementation only calls Dispose() on any objects that it stores in its fields that are disposable. Which in turn release unmanaged resources. If you implement IDisposable and not actually do anything, like you did in your snippet, then the object state doesn't change at all. Nothing can go wrong. Don't mix up disposal with finalization.

提交回复
热议问题