I think in the OP's case he can do pretty much what he wants to since
it's his object and his interface and judging by the names in his
example there is a 1-to-1 match. Probably he has made the interface so
that he can use the full range of methods on the class internally in the
assembly and yet expose a set of those methods to other assemblies
through an interface.
In this case he can just as well specify that his interface also brings
IDisposable into the game since it's his object.
On a general basis, unless the interface contract (documentation) states
that there is resources that needs to be disposed of in every
implementation of that interface, I would not do it and instead document it.
<snip>
> I have to disagree. Interface inheratence does not define an "IS A"
> relationship, but rather an "OPERATES LIKE" relationship. Inheriting from
> IDisposable simply indecates that implementing classes will likely have a
> Create, Use, Cleanup lifecycle.
Implementing classes *will have* a cleanup method, but it can of course
be empty.
> But more importantly, he's missing the most important thing about disposable
> types. With any disposable type the first-order concern is making sure that
> client code actually disposes the instances!
And you won't get the compiler to enforce that no matter what you do.
The closest thing you'll get is to provide a finalizer that throws an
exception because it obviously should never be executed.
> If you are difining an interface it is likely that conusmer code will not
> know about the actual implementation. So if you hide the fact that the type
> is disposable in the concrete implementation then you increase the
> possibility that client code will use the type without disposing it. if
> IMyObject doesn't inherit from IDisposable then this code:
There could be many gotchas with an object that you don't know of. There
is no way you will be able to talk the compiler into enforcing all of
these. The only good way to make sure the object is used correctly is to
provide a good set of documentation for it.
> void DoSomething()
> {
[quoted text clipped - 6 lines]
> who wrote the above, incorrect code, would rightly ask,
> "Well how the hell was I supposed to know that IMyObject was disposable?".
He should've read the documentation. Not everything can be inferred from
reading method names and class signatures.
> The only way would by to put that stupid little "is IDisposable" test on
> absulutely every use of an object through an interface or non-sealed type
> variable on the off chance that the concrete run-time type is disposable.
If you're going to be paranoid about IDisposable, then yes. There's also
such a thing as future-proof. What if you use a class in your current
project and it doesn't implement IDisposable, and tomorrow you upgrade
to the latest version. The compiler won't tell you that you're now
supposed to dispose of the object but you have introduced a problem anyway.
Would I write code to make sure every such use would be future-proof? No.

Signature
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:lasse@vkarlsen.no
PGP KeyID: 0x2A42A1C2