Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested using statements

As Eric Gunnerson shows in this blog post, in C# you can nest using statements as:

using (StreamWriter w1 = File.CreateText("W1")) using (StreamWriter w2 = File.CreateText("W2")) {     // code here } 

Is there a similar way to do it in VB.Net? I want to avoid too many indentation levels.

like image 688
Hans Olsson Avatar asked Jul 27 '10 15:07

Hans Olsson


People also ask

Can we use nested using in C#?

Nesting of a while loop is allowed, which means you can use while loop inside another while loop. However, it is not recommended to use nested while loop because it is difficult to maintain and debug. Example: C#

What is using() in C#?

The using statement causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and can't be modified or reassigned. A variable declared with a using declaration is read-only.

Why use using in C#?

The using statement is used to set one or more than one resource. These resources are executed and the resource is released. The statement is also used with database operations. The main goal is to manage resources and release all the resources automatically.

What does a using statement do?

The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.


1 Answers

Like this:

Using a As New Thingy(), _       b As New OtherThingy()         ... End Using 
like image 172
SLaks Avatar answered Oct 04 '22 06:10

SLaks