There are a couple things wrong here, but I believe the root is that you might confusing IDisposable
with a Garbage Collector.
IDisposable
is an interface that the type A
, in your example, may implement. It that were the case, you could be sure that any instance of A
had the method Dispose()
you're looking for. This is useful for things like Stream
s, where they need to be closed, but it's not useful for (just as an example) a ComplexNumber
type that has no outstanding resources.
In these cases, your managed C# code will handle all the "disposal" that needs to happen. It will get rid of the memory that object is using. That feature comes for free (sort of), and you don't need (or want) to do anything to explicitly invoke it. That's the main difference between managed (C#, Java, etc.) and unmanaged (C++) code.
Essentially, if an object is using more than just memory (like an open file does), IDisposable
will be useful. If it is just memory (like a Form
, or DateTime
, or a majority of other things), there's no need for it.
As for your specific code, you've applied IDisposable
to the type that contains your functions. Just as you couldn't call this.AddMinutes(1)
to get at DateTime
's method, you can't get at this one.
If you do need the added functionality of implementing IDisposable
, you'll have to do it on A
. Applying IDisposable
, like any other interface, to MainclassForm
will tell the compiler that you have a Dispose()
method on that class, which is true only because the Windows Forms object has one. If it didn't, this would throw a compiler error, and you'd have to add one.