Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any benefit to implementing IDisposable on classes which do not have resources?

Tags:

c#

idisposable

In C#, if a class, such as a manager class, does not have resources, is there any benefit to having it : IDisposable?

Simple example:

public interface IBoxManager {  int addBox(Box b); }  public class BoxManager : IBoxManager {  public int addBox(Box b)  {   using(dataContext db = new dataContext()){    db.Boxes.add(b);    db.SaveChanges();   }   return b.id;  } } 

Will there be any benefit in memory use when using BoxManager if it also implements IDisposable? public class BoxManager : IBoxManager , IDisposable

For example:

BoxManager bm = new BoxManager(); bm.add(myBox); bm.dispose();//is there benefit to doing this? 
like image 474
Travis J Avatar asked Feb 27 '12 21:02

Travis J


People also ask

Which of these are the reason for implementing IDisposable interface?

If your class creates unmanaged resources, then you can implement IDisposable so that these resources will be cleaned up properly when the object is disposed of. You override Dispose and release them there.

When should you use IDisposable?

You should implement IDisposable when your class holds resources that you want to release when you are finished using them. Show activity on this post. When your class contains unmanaged objects, resources, opened files or database objects, you need to implement IDisposable .

Why do we need IDisposable?

You never know when the garbage collector will collect your object. You don't even know if it even will (unlike using delete in C++, which is deterministic). So IDisposable is there for deterministically releasing unneeded references (and releasing unmanaged resources).

What is the best approach to ensure that an IDisposable object releases the allocated resources?

The documentation of Object. To ensure deterministic release of resources for instances of your class, implement a Close method or provide a IDisposable. Dispose implementation. This is the virtue of using Dispose to cleanup unmanaged resources; you get to know, and control, when unmanaged resource are cleaned up.


1 Answers

There are only 2 reasons for implementing IDisposable on a type

  • The type contains native resources which must be freed when the type is no longer used
  • The type contains fields of type IDisposable

If neither of these are true then don't implement IDisposable

EDIT

Several people have mentioned that IDisposable is a nice way to implement begin / end or bookended operations. While that's not the original intent of IDisposable it does provide for a very nice pattern.

class Operation {   class Helper : IDisposable {     internal Operation Operation;     public void Dispose() {       Operation.EndOperation();     }   }   public IDisposable BeginOperation() {     ...     return new Helper() { Operation = this };   }   private void EndOperation() {     ...   } } 

Note: Another interesting way to implement this pattern is with lambdas. Instead of giving an IDisposable back to the user and hoping they don't forget to call Dispose have them give you a lambda in which they can execute the operation and you close out the operation

public void BeginOperation(Action action) {   BeginOperationCore();   try {     action();   } finally {     EndOperation();   } } 
like image 149
JaredPar Avatar answered Oct 20 '22 01:10

JaredPar