Champion: Implicitly scoped using statement (16.3, Core 3)
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`.
Posted by HaloFour on Feb 15, 2017, 2:41 AM
Tags
Related issues
Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments
github (E-commerce)
proposal: cmd/cgo: cgo without a C toolchain
Replace PowerToys Run with Command Palette
VPS with a vRack leg
Possibility of Portal 1 Code Inclusion
Introducing NewPlayer
Metrics for PRs are never expiring ("memory leak")
ship even more of the compiler in source form
Flatpak Application TRACKING ISSUE
Champion: Implicitly scoped using statement (16.3, Core 3)
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`.
Comments (0)
No comments yet. Start the discussion.