Dispose question

后端 未结 7 1832
無奈伤痛
無奈伤痛 2021-02-14 02:17

I have a number of classes which have private member variables that implement IDisposable (timers, brushes, etc). Do I need to do anything to ensure these variables are cleaned

7条回答
  •  不思量自难忘°
    2021-02-14 03:06

    You have some good information and some misinformation in your understanding.

    The long and short of it is that you need to Dispose() anything that implements IDisposable.

    Being as these are private member variables of your class, and if these are supposed to be available for the lifetime of instances of that class, your class should also implement IDisposable and Dispose() of those types in its own Dispose() method.

    If those private member variables have a limited lifetime (i.e. only within one method), just wrap them in a using block.

提交回复
热议问题