Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are all disposable objects instantiated within a using block disposed?

This is a question I have asked myself many times in the past as I nested using statements 5 deep.

Reading the docs and finding no mention either way regarding other disposables instantiated within the block I decided it was a good Q for SO archives.

Consider this:

using (var conn = new SqlConnection())
{
    var conn2 = new SqlConnection();
}

// is conn2 disposed?
like image 237
Sky Sanders Avatar asked Feb 17 '10 22:02

Sky Sanders


1 Answers

No they are not. Only the set of variables explicitly listed in the using clause will be automatically disposed.

like image 127
JaredPar Avatar answered Oct 24 '22 23:10

JaredPar