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.
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#
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.
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.
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.
Like this:
Using a As New Thingy(), _ b As New OtherThingy() ... End Using
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With