What's the purpose of GC.SuppressFinalize(this) in Dispose() method?

后端 未结 5 380
北荒
北荒 2020-12-04 13:47

I have the following code:

public void Dispose()
{
    if (_instance != null)
    {
        _instance = null;
        // Call GC.SupressFinalize to take this         


        
5条回答
  •  隐瞒了意图╮
    2020-12-04 14:14

    From MSDN: GC.SuppressFinalize:

    This method sets a bit in the object header, which the system checks when calling finalizers. The obj parameter is required to be the caller of this method.

    Objects that implement the IDisposable interface can call this method from the IDisposable..::.Dispose method to prevent the garbage collector from calling Object..::.Finalize on an object that does not require it.

    Typically you would use this if your object does not reference other objects, just discrete types, or has already reset any object references to NULL.

提交回复
热议问题