6

For example

using(var something = GetSomething())
{
    something.DoSomething();
    if(something.IsX()) return true;
}
return false;
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
  • Hmm, that's an interesting one, you're doing a GetSomething() and not a New Something. I don't think the framework has intelligence to distinguish the diff. If Something is a singleton you might get "DisposedObject" exceptions after exiting. You could build a small test app to find out what the framework does with a Using(GetSomething()). – Louis Somers Jun 15 '11 at 11:00

6 Answers6

8

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

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.

mdm
  • 12,480
  • 5
  • 34
  • 53
1

Yes, using is a compiler feature, which expands to

try {
  ...
  return ...;
}
finally {
  foo.Dispose();
}
Optillect Team
  • 1,737
  • 2
  • 11
  • 12
0

As far as I know the using block will dispose of the object as soon as it exits scope.

Eg, when true is returned, the next statement is outside of scope so the variable will be disposed.

WraithNath
  • 17,658
  • 10
  • 55
  • 82
0

I think as soon as the control will go out of {} dispose will be called so in short Yes

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
0

One point not yet mentioned is that while a "return" within a "using" block will call Dispose on the controlled variable, a "yield return" from within a "using" block within an iterator will not Dispose the controlled variable unless the IEnumerator associated with the iterator is Disposed.

supercat
  • 77,689
  • 9
  • 166
  • 211