Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested using blocks

Tags:

c#

.net

using

I knew we could create more than one instance of the same type in a using block! but is there a way i could have different types instances nested or written in a single using block?

i just read this, so far it seems the only option http://blogs.msdn.com/b/ericgu/archive/2004/08/05/209267.aspx

like image 425
ioWint Avatar asked Aug 17 '11 21:08

ioWint


2 Answers

It's not possible to have variables of different declared types in the same using statement. The C# spec limits the set of valid constructs to a single expression or a local variable declaration. The latter is covered in section 8.5.1 of the C# lang spec and only provides for a single variable type

local-variable-declaration:
  local-variable-type local-variable-declarators

To support different local variable types you need to use some form of nesting. For example

using (Type1 local1 = new Type1(), local2 = new Type1())
using (Type2 local3 = new Type2(), local4 = new Type2())
{

}
like image 170
JaredPar Avatar answered Nov 03 '22 12:11

JaredPar


No. It is similar to the following:

int a, b;

Both a and b are int - that is it.

like image 22
IAbstract Avatar answered Nov 03 '22 14:11

IAbstract