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:
xxxxxxxxxx
1
using (var stream = new FileStream("file.txt", FileMode.Open))
2
using (var reader = new StreamReader(stream))
3
// add more usings here ...
4
{
5
string line = reader.ReadLine();
6
7
// ...
8
}
In C# 8 we can join usings by adding coma as variables separators.
xxxxxxxxxx
1
using (
2
var stream = new FileStream("file.txt", FileMode.Open),
3
var reader = new StreamReader(stream)
4
// add more usings here ...
5
)
6
{
7
string line = reader.ReadLine();
8
9
// ...
10
}