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?
There are only 2 reasons for implementing
IDisposableon a typeIDisposableIf neither of these are true then don’t implement
IDisposableEDIT
Several people have mentioned that
IDisposableis a nice way to implement begin / end or bookended operations. While that’s not the original intent ofIDisposableit does provide for a very nice pattern.Note: Another interesting way to implement this pattern is with lambdas. Instead of giving an
IDisposableback to the user and hoping they don’t forget to callDisposehave them give you a lambda in which they can execute the operation and you close out the operation