1

I've been reading about IDisposable interface lately (this topic was quite useful Proper use of the IDisposable interface) and also about usage of using statement (topic Uses of "using" in C#). Then I started wondering what if I should somehow free my own collection class from memory.

class User{
    private string username {set;get;}
    private string password {set;get;}
    ...
}

Should it implement IDisposable interface?

class User : IDisposable
{
    ...
    public void Dispose()
    {
        this.Dispose();
    }
}

so it could be freed from memory? Or does GC do it automaticly and I shouldn't even bother about it.

As far as I understand it's important to free unmanaged resources like DB connections and such but what about those collection classes. Since I use them quite frequently It really started to bug me.

tl;dr; should I implement IDisposable on User class?

Kind Regards.


edit: thanks everyone for the replies!

Community
  • 1
  • 1
Dashovsky
  • 137
  • 9
  • 2
    You need to implement `IDisposable` only if your class allocates unmanaged resources and/or has `IDisposable` class members. So, in your case the answer is "No", you don't need to make this class disposable. – Alex F Apr 13 '15 at 13:45

4 Answers4

6

Or does GC do it automaticly and I shouldn't even bother about it.

This. Unless you have unmanaged resources (either directly or by way of a reference to something else which is disposable), you almost certainly shouldn't implement IDisposable.

Your current implementation would just call itself:

public void Dispose()
{
    this.Dispose();
}

... so assuming you don't really want to call this.Dispose(), what would you want to do when Dispose() is called? It's not like disposal causes garbage collection - so what action do you want to take? If the answer is "nothing" then you probably shouldn't be implementing IDisposable. (The exception here is if this is designed to be a base class and you're expecting some derived classes to require disposal... that's a more complex scenario.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

All unused resources will be Garbage collected eventually when the program unloads or after separate interval if the resources are no longer used.

Its a better practice to clean/dispose used resources(variables, objects) when you no longer wish to use them to free memory.

sudhansu63
  • 6,025
  • 4
  • 39
  • 52
0

As said in the other answers, the only times you have to implement IDisposable is when you deal with unmanaged resources or class members that are IDisposable themselves (in this case you should dispose them in your own Dispose method).

Another scenario you might see is when people want to use the using { } syntactic sugar to automatically call some method at the end of the variable scope without using the try { } finally { } form.

I.e.:

public class MyObject : IDisposable
{
    public void Foo()
    {
    }

    public void Dispose()
    {
        // Something they want to call after the use of an instance of MyObject
    }
}
...
using (var myObj = new MyObject())
{
    myObj.Foo();
}

instead of

public class MyObject
{
    public void Foo()
    {
    }

    public void MethodToCallAfterUse()
    {
        // Something they want to call after the use of an instance of MyObject
    }
}

var myObj = new MyObject(); 
try
{
    myObj.Foo();
}
finally
{
    myObj.MethodToCallAfterUse();
}
ken2k
  • 48,145
  • 10
  • 116
  • 176
0

If an object asks an outside entity to "do something" (perform an action, reserve a resource, etc.) on its behalf until further notice, and that outside entity might continue to exist after the object requesting its services has ceased to be useful, then the object should ensure that the outside entity receives notice when its services are no longer required. The IDisposable resource exists as a standard means by which an object can say "I may be obligated to let outside entities know when I don't need their services; let me know when my services will no longer be needed, so I can satisfy my obligation to tell any outside entities that I no longer need their services."

In .NET, once no references to an object exist anywhere in the universe, the object itself will likewise cease to exist. If the only object which knows of an obligation to do perform some action ceases to exist without having performed the action, the action will not get performed. If an object has no obligations, however, having it simply cease to exist once there are no references is just fine.

supercat
  • 77,689
  • 9
  • 166
  • 211