Nested Usage Blocks

I knew that we could create multiple instances of the same type in a use block! but is there a way that I could have different types of instances nested or written in the same use block?

I just read this while this is the only option http://blogs.msdn.com/b/ericgu/archive/2004/08/05/209267.aspx

+6
source share
2 answers

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

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

To support different local types of variables, you need to use some form of nesting. for instance

 using (Type1 local1 = new Type1(), local2 = new Type1()) using (Type2 local3 = new Type2(), local4 = new Type2()) { } 
+4
source

Not. This is similar to the following:

 int a, b; 

Both a and b are int - this is it.

+1
source

Source: https://habr.com/ru/post/895283/


All Articles