C# USING keyword - when and when not to use it?

后端 未结 3 1449

I\'d like to know when i should and shouldn\'t be wrapping things in a USING block.

From what I understand, the compiler translates it into a try/finally, where the

相关标签:
3条回答
  • 2020-12-05 07:39

    No, IDisposable items are not disposed when they go out of scope. It is for precisely this reason that we need IDisposable - for deterministic cleanup.

    They will eventually get garbage collected, and if there is a finalizer it will (maybe) be called - but that could be a long time in the future (not good for connection pools etc). Garbage collection is dependent on memory pressure - if nothing wants extra memory, there is no need to run a GC cycle.

    Interestingly (perhaps) there are some cases where "using" is a pain - when the offending class throws an exception on Dispose() sometimes. WCF is an offender of this. I have discussed this topic (with a simple workaround) here.

    Basically - if the class implements IDisposable, and you own an instance (i.e. you created it or whatever), it is your job to ensure that it gets disposed. That might mean via "using", or it might mean passing it to another piece of code that assumes responsibility.

    I've actually seen debug code of the type:

    #if DEBUG
        ~Foo() {
            // complain loudly that smoebody forgot to dispose...
        }
    #endif
    

    (where the Dispose calls GC.SuppressFinalize)

    0 讨论(0)
  • 2020-12-05 07:44

    To add to the other answers, you should use using (or an explicit Dispose) whenever an object holds any resources other than managed memory. Examples would be things like files, sockets, database connections, or even GDI drawing handles.

    The garbage collector would eventually finalise these objects, but only at some unspecified time in the future. You can't rely on it happening in a timely fashion, and you might have run out of that resource in the meantime.

    0 讨论(0)
  • 2020-12-05 07:52

    "Are IDisposables not disposed of when they go out of scope?"

    No. If the IDisposable object is finalizable, which is not the same thing, then it will be finalized when it's garbage collected.

    Which might be soon or might be almost never.

    Jeff Richter's C#/CLR book is very good on all this stuff, and the Framework Design Guidelines book is also useful.

    Do I only need to use a USING when my object makes use of Dispose to tidy itself up?

    You can only use 'using' when the object implements IDisposable. The compiler will object if you try to do otherwise.

    0 讨论(0)
提交回复
热议问题