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

后端 未结 11 2077
挽巷
挽巷 2020-12-14 05:14

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

Simple example:

pu         


        
相关标签:
11条回答
  • 2020-12-14 05:54

    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.

    0 讨论(0)
  • 2020-12-14 06:01

    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.

    0 讨论(0)
  • 2020-12-14 06:02

    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();
      }
    }
    
    0 讨论(0)
  • 2020-12-14 06:05

    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.
    0 讨论(0)
  • 2020-12-14 06:06

    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.

    0 讨论(0)
  • 2020-12-14 06:11

    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.

    0 讨论(0)
提交回复
热议问题