Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will a IDisposable be disposed if the using block returns?

For example

using(var something = GetSomething())
{
    something.DoSomething();
    if(something.IsX()) return true;
}
return false;
like image 455
Louis Rhys Avatar asked Jun 15 '11 10:06

Louis Rhys


People also ask

What happens if you dont Dispose IDisposable?

IDisposable is usually used when a class has some expensive or unmanaged resources allocated which need to be released after their usage. Not disposing an object can lead to memory leaks.

Does using automatically Dispose?

Using-blocks are convenient way to automatically dispose objects that implement IDisposable interface to release resources that garbage collection cannot automatically manage.

How does IDisposable work?

In a nutshell, an IDisposable class allows you to explicitly handle the deallocation of resources (typically unmanaged resources or database connections) via the Dispose() method. IDisposable class instances should be created within a "Using" block so as to ensure that the Dispose method is actually called.


2 Answers

Yes, absolutely. The Dispose method is called however the using statement is executed, unless it was an abrupt whole-process termination. The most common cases are:

  • A return within the block
  • An exception being thrown (and not caught) within the block
  • Reaching the end of the block naturally

Basically a using statement is mostly syntactic sugar for a try/finally block - and finally has all the same properties.

EDIT: From section 8.13 of the C# 4 specification:

A using statement is stranslated into three parts: acquisition, usage, and disposal. Usage of the resource is implicitly enclosed in a try statement that includes a finally clause. This finally clause disposes of the resource.

The finally statement is described in section 8.10 of the specification:

The statements of a finally block are always executed when control leaves a try statement. This is true whether the control transfer occurs as a result of normal execution; as a result of executing a break, continue, goto or return statement; or as a result of propagating an exception out of the try statement.

like image 118
Jon Skeet Avatar answered Sep 28 '22 18:09

Jon Skeet


Yes.

using is syntactic sugar for a try/finally block:

The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block;

In the documentation on the finally block:

Whereas catch is used to handle exceptions that occur in a statement block, finally is used to guarantee a statement block of code executes regardless of how the preceding try block is exited.

So, the using gets translated to try/finally, with .Dispose() in the finally part, ensuring that it is always executed no matter what happens in the try/catch.

like image 20
mdm Avatar answered Sep 28 '22 18:09

mdm