EN
C# - using statement with multiple variables
8
points
In this short article, we would liek to show how to use using
statement with multiple variables in C#.
Quick solution:
using (var stream = new FileStream("file.txt", FileMode.Open))
using (var reader = new StreamReader(stream))
// add more usings here ...
{
string line = reader.ReadLine();
// ...
}
C# 8 example
In C# 8 we can join usings by adding coma as variables separators.
using (
var stream = new FileStream("file.txt", FileMode.Open),
var reader = new StreamReader(stream)
// add more usings here ...
)
{
string line = reader.ReadLine();
// ...
}