1

Q1) Should I make all my classes like this so that I can use using (...)?

Ie, it this good practice?

public void Dispose()
    {
        // in its simplest form
    }

Q2) Other than code for specific objects I used that need closing, is there anything I should add to the Dispose() method?

NB1: I understand I must dispose of IDisposable classes by either of these two ways:

1) myClass.Dispose();

2) using (MyCalss myClass = new MyClass())

NB2: I have some classes that need to run for the full time of the app, so I won't make these IDisposable.

Mouheb
  • 41
  • 6
  • 2
    No, you should not have all your classes implement `IDiposable` as a matter of course. As with everything, you should choose to do something because you have a positive case that it's a good idea. – Kirk Woll Feb 07 '15 at 23:24
  • Good resources for understanding when to use IDisposable: http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface and https://social.msdn.microsoft.com/Forums/en-US/16c0d31a-d39c-4c37-96f8-98df3543e1d7/idisposable-why-and-when-to-use-it – Prescott Feb 07 '15 at 23:25
  • 1
    What advantage do you see in using `using ( ...)` (or what alternative do you see that you consider less desirable)? – O. R. Mapper Feb 07 '15 at 23:27

1 Answers1

0

No you don't have to make all your classes to implement the IDisposable interface. This should be done only if your class uses unmanaged resources.

According to MSDN:

The primary use of this interface is to release unmanaged resources. The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used. However, it is not possible to predict when garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams.

I have some classes that need to run for the full time of the app, so I won't make these IDisposable.

In this case, provided that, you don't want to create instances of these classes, a reasonable solution would be to make them static.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • 2
    I would not agree. IDisposable is useful for any kind of class that needs cleanup, even if the cleanup does not involve unmanaged resources. Of course, not all classes do need cleanup. But for those that do, IDisposable is the way to go. – Mike Nakis Feb 07 '15 at 23:30
  • @MikeNakis you argument makes sense. However, it's my personal preference to use this interface for situations where I have to release unmanaged resources. This doesn't mean that your approach is not correct. – Christos Feb 07 '15 at 23:33
  • I think is not good way to implement IDisposble to classes what do not contains unmanaged resources or resources inherits from IDisposable. If you want cleanup managed resources you can use GarbageCollector. But GC can not clean unmanaged resources so for this case is IDIsposable create and i think for nothing else. – Peter M. Feb 07 '15 at 23:38
  • 2
    Anything that requires deterministic cleanup is a good candidate for `IDisposable`. I use it a lot when I have a class that needs to have clean up immediately and I can't wait on the GC to get around to cleaning things up. – Steve Mitcham Feb 07 '15 at 23:42
  • 2
    @PeterM.: "Cleaning up" is not restricted to freeing memory. It can also mean something like "restoring settings", or "changing the state of some owner object", which the GC cannot possibly have any idea about. Of course, you may choose to interpret any of these as an "unmanaged resource". – O. R. Mapper Feb 07 '15 at 23:47