Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

dispose

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 finally calls Dispose() on the object.

I always use a USING around database connections and file access, but its more out of habit rather than a 100% understanding. I know you should explicity (or with a using) Dispose() objects which control resources, to ensure they are released instantly rather than whenever the CLR feels like it, but thats where my understanding breaks down.

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

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

Thanks

Edit: I know there are a couple of other posts on the USING keyword, but I'm more interested in answers relating the the CLR and exactly whats going on internally

Andrew

like image 408
Andrew Bullock Avatar asked Nov 25 '08 12:11

Andrew Bullock


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in coding language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

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)

like image 180
Marc Gravell Avatar answered Oct 07 '22 14:10

Marc Gravell


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

like image 40
Will Dean Avatar answered Oct 07 '22 16:10

Will Dean