Is there a better deterministic disposal pattern than nested “using”s?

前端 未结 10 1430
难免孤独
难免孤独 2021-02-18 18:23

In C#, if I want to deterministically clean up non-managed resources, I can use the \"using\" keyword. But for multiple dependent objects, this ends up nesting further and furt

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-18 18:48

    Instead of nesting using statements, you can just write out the .Dispose calls manually - but you'll almost certainly miss one at some point.

    Either run FxCop or something else that can make sure that all IDisposable-implementing type instances have a .Dispose() call, or deal with the nesting.

提交回复
热议问题