0

How to dispose my object? I am doing it by this. Whats is wrong? The Obj.Dispose() function does not appear to be right.

class MainclassForm : Form, IDisposeable
{
    public void createanddispose()
    {
        A obj = new A();
        obj.dowork();
        obj.Dispose();//does not appear as a function
    }
}
Arghya C
  • 9,805
  • 2
  • 47
  • 66
phpnet
  • 903
  • 1
  • 8
  • 23
  • IDisposeable, should not this be IDisposable. See this link as to how can you implement the interface https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx – qamar Dec 28 '15 at 06:48
  • What is type of `A`? In order for the object to be disposeable, it must implement IDisposable, – Ian Dec 28 '15 at 06:48
  • 1
    IDisposeable should be implemented by A class too ? – phpnet Dec 28 '15 at 06:52
  • @qamar I already had gone through that example and what I could understand is in my question. Can u please give answer according to my code ? – phpnet Dec 28 '15 at 06:53
  • @phpnet because the question is about obj.Dispose() does not appear as function... suppose A is `string` or `object` and obj is of type A. Then if you type obj.Dispose() - the method won't be shown there by intellisense (because there isn't any for non-disposable object) – Ian Dec 28 '15 at 07:17

3 Answers3

2

You can better use the using statement. Something like

using (MyIDisposableObject obj = new MyIDisposableObject())
{
    // object here
}

A good reference to check on MSDN: Avoiding Problems with the Using Statement

The C# "using" statement results in a call to Dispose(). This is the same as Close(), which may throw exceptions when a network error occurs. Because the call to Dispose() happens implicitly at the closing brace of the "using" block, this source of exceptions is likely to go unnoticed both by people writing the code and reading the code. This represents a potential source of application errors.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

To call Dispose() on an object, your class must be inherited from IDisposeable interface and have an implementation of it:

class A : IDisposable
{
   public void Dispose()
   { 
      GC.Collect();           
   }
}
Shaharyar
  • 12,254
  • 4
  • 46
  • 66
  • 1
    It might be worth [checking this out](http://stackoverflow.com/questions/1149197/gc-collect) before we throw `GC.Collect()` in too many places. It's a pretty specialized function, and probably not suitable for this use-case. It might be, I just wouldn't default to it. – Matthew Haugen Dec 28 '15 at 07:00
0

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 Streams, 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.

Matthew Haugen
  • 12,916
  • 5
  • 38
  • 54