What's the purpose of implementing the IDisposable interface?

后端 未结 5 1075
无人及你
无人及你 2021-01-13 13:05

What\'s the purpose of implementing the IDisposable interface? I\'ve seen some classes implementing it and I don\'t understand why.

5条回答
  •  日久生厌
    2021-01-13 13:58

    When your classes makes use of some system resource, it's the class' responsibility to make sure the resource is freed too. By .Net design you're supposed to do that in the Dispose method of the class. The IDisposable interface marks that your class needs to free resource when it's no longer in use, and the Dispose method is made available so that users of your class can call it to free the consumed resources.

    The IDisposable method is also essential if you want auto clean-up to work properly and want to use the using() statement.

提交回复
热议问题