Why is it always necessary to implement IDisposable on an object that has an IDisposable member?

后端 未结 7 555
耶瑟儿~
耶瑟儿~ 2020-12-18 09:55

From what I can tell, it is an accepted rule that if you have a class A that has a member m that is IDisposable, A should implement IDisposable and it should call m.Dispose(

7条回答
  •  轮回少年
    2020-12-18 10:15

    But if Image has followed conventions and implemented a finalizer and I don't care about the timely release of resources, what's the point?

    Then there isn't one, if you don't care about timely release, and you can ensure that the disposable object is written correct (in truth I never make an assumption like that, not even with MSs code. You never know when something accidentally slipped by). The point is that you should care, as you never know when it will cause a problem. Think about an open database connection. Leaving it hanging around, means that it isn't replaced in the pool. You can run out if you have several requests come in for one.

    Nothing says you have to do it if you don't care. Think of it this way, it's like releasing variables in an unmanaged program. You don't have to, but it is highly advisable. If for no other reason the person inheriting from the program doesn't have to wonder why it wasn't taken care of and then try and clear it up.

提交回复
热议问题