Home » c# » Is there any benefit to implementing IDisposable on classes which do not have resources?

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

Posted by: admin November 29, 2017 Leave a comment

Questions:

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?
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();
  }
}

Questions:
Answers:

There won’t be a scrap of difference between the disposable and non-disposable version if you don’t explicitly make use of the Dispose() method.

Questions:
Answers:

While your code wouldn’t benefit from implementing IDisposable, I can’t agree with other opinions here that state that IDisposable is only meant to (directly or indirectly) free native resources. IDisposable can be used whenever the object needs to perform clean up task at the end of it’s lifetime span. It’s extremely useful together with using.

A very popular example: in ASP.NET MVC Html.BeginForm returns an IDisposable. On creation, the object opens the tag, when Dispose is called it closes it. No native resources involved, yet still a good use of IDisposable.

Questions:
Answers:

No, there will be no benefit if you don’t do something useful like releasing unmanaged resources that your class might hold in the Dispose method.

Questions:
Answers:

One major point of confusion, which may not be applicable in your case but arises often, is what exactly constitutes a “resource”. From the perspective of an object, an unmanaged resource is something which an outside entity () is “doing”(*) on its behalf, which that outside entity will keep doing–to the detriment of other entitites–until told to stop. For example, if an object opens a file, the machine which hosts the file may grant that object exclusive access, denying everyone else in the universe a chance to use it unless or until it gets notified that the exclusive access isn’t needed anymore.

(*) which could be anything, anywhere; possibly not even on the same computer.

(**) or some way in which the the behavior or state of an outside entity is altered

If an outside entity is doing something on behalf of an object which is abandoned and disappears without first letting the entity know its services are no longer required, the outside entity will have no way of knowing that it should stop acting on behalf of the object which no longer exists. IDisposable provides one way of avoiding this problem by providing a standard means of notifying objects when their services are not required. An object whose services are no longer required will generally not need to ask any further favors from any other entities, and will thus be able to request that any entities that had been acting on its behalf should stop doing so.

To allow for the case where an object gets abandoned without IDisposable.Dispose() having been called first, the system allows objects to register a “failsafe” cleanup method called Finalize(). Because for whatever reason, the creators of C# don’t like the term Finalize(), the language requires the use of a construct called a “destructor” which does much the same thing. Note that in general, Finalize() will mask rather than solve problems, and can create problems of its own, so it should be used with extreme caution if at all.

A “managed resource” is typically a name given to an object which implements IDisposable and usually, though not always, implements a finalizer.

Questions:
Answers:

No, if there are no (managed or unmanaged) resources there is no need for IDisposable either.

Small caveat: some people use IDisposable to clean up eventhandlers or large memory buffers but

  • you don’t seem to use those
  • it’s a questionable pattern anyway.
Questions:
Answers:

From my personal experience (confirmed with discussion and other posts here) I would say, that there could be a situations where your object use massive amount of events, or not massive amount but frequent subscriptions and unsubscription from the event which sometimes leads to that the object is not garbage collected. In this case I in Dispose unsubscribe from all events my object subscribed before.

Hope this helps.

Questions:
Answers:

IDisposable is also great if you want to benefit the using () {} syntax.

In a WPF project with ViewModels, I wanted to be able to temporarily disable NotifyPropertyChange events from raising. To be sure other developers will re-enable notifications, I wrote a bit of code to be able to write something like:

using (this.PreventNotifyPropertyChanges()) {
    // in this block NotifyPropertyChanged won't be called when changing a property value
}

The syntax looks okay and is easily readable. For it to work, there’s a bit of code to write. You will need a simple Disposable object and counter.

public class MyViewModel {
    private volatile int notifyPropertylocks = 0; // number of locks

    protected void NotifyPropertyChanged(string propertyName) {
        if (this.notifyPropertylocks == 0) { // check the counter
            this.NotifyPropertyChanged(...);
        }
    }

    protected IDisposable PreventNotifyPropertyChanges() {
        return new PropertyChangeLock(this);
    }

    public class PropertyChangeLock : IDisposable {
        MyViewModel vm;

        // creating this object will increment the lock counter
        public PropertyChangeLock(MyViewModel vm) {
            this.vm = vm;
            this.vm.notifyPropertylocks += 1;
        }

        // disposing this object will decrement the lock counter
        public void Dispose() {
            if (this.vm != null) {
                this.vm.notifyPropertylocks -= 1;
                this.vm = null;
            }
        }
    }
}

There are no resources to dispose here. I wanted a clean code with a kind of try/finally syntax. The using keyword looks better.

Questions:
Answers:

is there any benefit to having it : IDisposable?

It doesn’t look so in your specific example, however: there is one good reason to implement IDisposable even if you don’t have any IDisposable fields: your descendants might.

This is one of the big architectural problems of IDisposable highlighted in IDisposable: What your mother never told you about resource deallocation. Basically, unless your class is sealed you need to decide whether your descendants are likely to have IDisposable members. And this isn’t something you can realistically predict.

So, if your descendants are likely to have IDisposable members, that might be a good reason to implement IDisposable in the base class.

Questions:
Answers:

Short answer would be no. However, you can smartly use the nature of the Dispose() executement at the end of the object lifecycle. One have already gave a nice MVC example (Html.BeginForm)

I would like to point out one important aspect of IDisposable and using() {} statement. At the end of the Using statement Dispose() method is automatically called on the using context object (it must have implemented IDisposable interface, of course).

Questions:
Answers:

There one more reason that no one mentioned (though it’s debateful if it really worth it):
The convension says that if someone uses a class that implement IDisposable, it must call its Dispose method (either explicitly or via the ‘using’ statement).
But what happens if V1 of a class (in your public API) didn’t need IDisposable, but V2 does? Technically it doesn’t break backward compatibility to add an interface to a class, but because of that convension, it is! Because old clients of your code won’t call its Dispose method and may cause resources to not get freed.
The (almost) only way to avoid it is to implement IDisposable in any case you suspect that you’ll need it in the future, to make sure that your clients always call your Dispose method, that some day may be really needed.
The other (and probably better) way is to implemet the lambda pattern mentioned by JaredPar above in the V2 of the API.