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

Given this code in the dispose-pattern from msdnpublic class DisposableResourceHolder : IDisposable {p

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).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744413734a4573011.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信