Yes, it will close the connection.
The entire point of a using
is that it will dispose the object when you leave the scope of the using
, even if it is through an exception.
a using
block is, under the hood, implemented using a try/finally
block.
This is easy enough to test experimentally as well:
public class Foo : IDisposable
{
public void Dispose()
{
Console.WriteLine("I was disposed!");
}
}
private static void Main(string[] args)
{
try
{
using (var foo = new Foo())
throw new Exception("I'm mean");
}
catch { }
}
And the output is:
I was disposed!