Champion: Implicitly scoped using statement (16.3, Core 3)
- Dominant language
- C#
- Stars
- 12.7k
- Forks
- 1.1k
- Avg merge
- 11h 1m
- Merged PRs (30d)
- 3
Description
See https://github.com/dotnet/roslyn/issues/5881 and https://github.com/dotnet/roslyn/issues/181
## Motivation (TL;DR edition):
Allow `using` keyword to be used to declare a variable that will be disposed at the end of the current block. This avoids creating a new block with an additional level of indentation:
### Before:
```cs
public void Foo() {
using (var connection = new SqlConnection(connectionString)) {
connection.Open();
using (var command = connection.CreateCommand()) {
command.CommandText = "SELECT FOO FROM BAR";
using (var reader = command.ExecuteReader()) {
while (reader.Read()) {
ProcessRecord(reader);
}
}
}
}
}
```
### After:
```cs
public void Foo() {
using var connection = new SqlConnection(connectionString));
connection.Open();
using var command = connection.CreateCommand());
command.CommandText = "SELECT FOO FROM BAR";
using var reader = command.ExecuteReader());
while (reader.Read()) {
ProcessRecord(reader);
}
}
```
As an implementation detail, the variables would be disposed in the reverse order in which they are declared. So given the above code, the order in which the resources would be disposed is `reader`, `command` and then `connection`.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.