Implementing RAII in C#

前端 未结 4 1519
一整个雨季
一整个雨季 2021-01-18 00:47

I have an InfoPath form which I need to conditionally disable it\'s OnChange events. Since it\'s not possible to bind the event handlers after the form has loaded, I\'m forc

4条回答
  •  深忆病人
    2021-01-18 01:12

    No and no. using is the closest you can get to RAII (more accurately, we are talking about the resource release that follows a RAII-idiom object being destructed).

    To answer your points more directly:

    1. IDisposable (and by extension using) was created exactly because there is no way to do that in .NET.
    2. using is syntactic sugar that gets compiled as try/finally and only requires that the object is IDisposable, so you cannot distinguish between usage inside a using statement and out of it.

提交回复
热议问题