Dispose question

后端 未结 7 1975
北荒
北荒 2021-02-14 02:51

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:22

    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.

提交回复
热议问题