How to dispose managed resource in Dispose() method in C#?

前端 未结 4 1923
自闭症患者
自闭症患者 2020-12-16 16:33

I know Dispose() is intended for unmanaged resource, and the resource should be disposed when it is no longer needed without waiting for the garbage collector to finalize th

4条回答
  •  无人及你
    2020-12-16 17:36

    Maybe a little clearer. GC.SuppressFinalize(this) only affects the object referenced by the this pointer but not to any members of the object. That is to say that SuppressFinalize does not recursively apply to the object's members. When the Garbage Collector reclaims the memory for the Disposed object, it is likely that there will be no active references to the objects fields. Since you did not call GC.SuppressFinalize on all the fields of the object, then the Garbage Collector will call the finalize method on these objects if they exist. When it does this is completely up to the runtime and in general you should just let it do its thing.

提交回复
热议问题