最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Does the Dispose pattern without a finalizer make any sense? - Stack Overflow

programmeradmin5浏览0评论

Given this code in the dispose-pattern from msdn

public class DisposableResourceHolder : IDisposable {

    private SafeHandle resource; // handle to a resource

    public DisposableResourceHolder() {
        this.resource = ... // allocates the resource
    }

    public void Dispose() {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing) {
        if (disposing) {
            if (resource!= null) resource.Dispose();
        }
    }
}

Why would one call GC.SuppressFinalize(this) if the object does not have a finalizer? If my understanding is right, the object won't even reach a finalization queue to be removed from in the first place.

And if the call to GC.SuppressFinalize(this) is not required and we remove it then the whole pattern's benefits becomes less obvious to me. What am I missing, why is Microsoft recommending this?

Given this code in the dispose-pattern from msdn

public class DisposableResourceHolder : IDisposable {

    private SafeHandle resource; // handle to a resource

    public DisposableResourceHolder() {
        this.resource = ... // allocates the resource
    }

    public void Dispose() {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing) {
        if (disposing) {
            if (resource!= null) resource.Dispose();
        }
    }
}

Why would one call GC.SuppressFinalize(this) if the object does not have a finalizer? If my understanding is right, the object won't even reach a finalization queue to be removed from in the first place.

And if the call to GC.SuppressFinalize(this) is not required and we remove it then the whole pattern's benefits becomes less obvious to me. What am I missing, why is Microsoft recommending this?

Share Improve this question asked Mar 20 at 11:19 meJustAndrewmeJustAndrew 6,64310 gold badges56 silver badges87 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 5

The only reason to implement the "full" Dispose pattern here is in case your class has a subclass which needs it. If this happens, the subclass will override Dispose(bool) and implement its own finalizer.

If you don't care about supporting this case (and most people don't, in practice), then you can just ditch GC.SuppressFinalize and Dispose(bool):

public sealed class DisposableResourceHolder : IDisposable {

    private SafeHandle resource; // handle to a resource

    public DisposableResourceHolder() {
        this.resource = ... // allocates the resource
    }

    public void Dispose() {
        resource.Dispose();
    }
}

(One school of thought recommends only ever using SafeHandle or equivalent to own unmanaged resources, which means that you almost never need to implement your own finalizer, which means you can just follow the simplified Dispose pattern above everywhere).

发布评论

评论列表(0)

  1. 暂无评论