When do you need to call IDisposable if you use `using` statements?

I read another answer. And that made me wonder when to call Dispose explicitly if I use using statements?

EDIT:

To justify myself with complete know-how, the reason I asked was that someone from another thread said something meaning that there was a good reason to call Dispose manually ... So I thought, why don't ask about it?

+4
source share
6 answers

No. The using statement does this for you.


According to MSDN , this code example:

 using (Font font1 = new Font("Arial", 10.0f)) { byte charset = font1.GdiCharSet; } 

when compiling, it expands to the following code (pay attention to additional curly brackets to create a limited area for the object):

 { Font font1 = new Font("Arial", 10.0f); try { byte charset = font1.GdiCharSet; } finally { if (font1 != null) ((IDisposable)font1).Dispose(); } } 

Note: As @timvw is mentioned , if you bind methods or use object initializers in the using declaration itself and throw an exception, the object will not be deleted. It makes sense if you look at what it will be expanded to. For instance:

 using(var cat = new Cat().AsDog()) { // Pretend a cat is a dog } 

expands to

 { var cat = new Cat().AsDog(); // Throws try { // Never reached } finally { if (cat != null) ((IDisposable)cat).Dispose(); } } 

AsDog will obviously make an exception, as a cat can never be as amazing as a dog. The cat will never be removed. Of course, some people may argue that cats should never be removed, but this is another discussion ...

In any case, just make sure that what you do using( here ) is safe and that you are good to go. (Obviously, if the constructor does not work, the object will not be created to begin with, so there is no need to dispose of it).

+20
source

Usually you do not. This is the point of the operator used. However, there is a situation where you have to be careful:

If you reassign the variable to another value, the using statement will only call the Dispose method on the original value.

 using (someValue = new DisposableObject()) { someValue = someOtherValue; } 

The compiler will even give you a warning about this:

Possibly incorrect assignment to the local "someValue", which is an argument in the use or lock statement. A call to Dispose or unlock will occur at the original local value.

+12
source

Be careful when using C # 3.0 object initializers. An example can be found here: http://ayende.com/Blog/archive/2009/01/15/avoid-object-initializers-amp-the-using-statement.aspx

+10
source

Never. It will output Dispose after the instructions inside the blocks are executed.

+5
source

The whole point of the operator used is that if your object implements IDisposable, dipoz will be called at the end of the code block. To do this, you need it to do it automatically for you.

+3
source

As far as I know

 using (var myDisposable = new MyDisposable()) { ... } 

basically converted by the compiler to

 var myDisposable = new MyDisposable() try { ... } finally { myDisposable.Dispose(); } 
+3
source

All Articles