3

Possible Duplicate:
How to check if object has been disposed in C#

all the agonies of mine are how can I check if an object, in .net for example an object of Bitmap, has been disposed or not yet. Shall I need to check if the property thrown an ObjectDisposedException to know if that object has been disposed or not.

I think that very dummy way to know, any one have a better way??

Community
  • 1
  • 1
Niklaus Wirth
  • 820
  • 1
  • 7
  • 21
  • A very good suggestion, but in bitmap case, you can use PixelFormat property :) as a very hacky way to know if the bitmap has been disposed :) – Saw Jan 03 '13 at 10:52
  • 1
    If you want to provide this property, it is better to make the Dispsable an abstractclass, and not an interface, because the behavior of IsDisposed is well known. but this makes the IDisposable not usable, it is better to be an interface – Saw Jan 03 '13 at 10:56
  • 1
    Friend Tim, In fact my question is more philosophic question than how can I implement it, In the real wold I have implemented an extension method to check if for example a Bitmap object has been disposed or not. – Niklaus Wirth Jan 03 '13 at 11:00
  • Philosophical questions are off-topic. – H H Jan 03 '13 at 11:06
  • Friend Mohamed, I don't think that if a property such IsDisposed had been added to the IDisposable interface would make it useless because in the underlying implementation there is _disposed private member which is help to make us call Dispose more than once without throwing an ObjectDisposedException, from my perspective I think that if _disposed member exposed publicly that would be great – Niklaus Wirth Jan 03 '13 at 11:07
  • Friend Henk, If that are the rules, I gave my apologies :) – Niklaus Wirth Jan 03 '13 at 11:09
  • @NiklausWirth: There is no need for `Dispose` to do anything at all except in cases where a class may have acquired and continued to hold resources that will need to be released once their services are no longer required. It is very common for classes which will never hold resources to have completely empty `Dispose` implementations. An object may thus have three disposal states: (1) Object is usable, holds resources, and requires disposal; (2) Object has released resources and is consequently useless; (3) Object is useful, but neither needs nor holds any resources for that purpose... – supercat Jun 18 '15 at 18:24
  • ...and doesn't care whether or not anyone calls `Dispose`. Before `Dispose` is called on them, objects in the third state need to be usable, and after `Dispose` is called on them, they shouldn't indicate that they still require disposable, but allowing a single `bool` property to satisfy both constraints would require wasting memory on an otherwise-useless flag indicating whether an object had been `Dispose`d. – supercat Jun 18 '15 at 18:28

0 Answers0