Why would a class implement IDisposable explicitly instead of implicitly?

后端 未结 4 2107
伪装坚强ぢ
伪装坚强ぢ 2020-12-01 23:57

I was using the FtpWebResponse class and didn\'t see a Dispose method. It turns out that the class implements IDisposable, but does so explicitly so that you must first cas

4条回答
  •  长情又很酷
    2020-12-02 00:14

    • Occasionally a class will have a Dispose method that is part of the interface but doesn't actually need to be called because the only resource to dispose of is memory: MemoryStream, for example.
    • As mentioned by others, if the class has a Close method that does the same thing as Dispose, arguably Dispose only needs to exist to support the "using" pattern so it may as well be explicit.

提交回复
热议问题