{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/5881","labels_url":"https://api.github.com/repos/dotnet/roslyn/issues/5881/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/roslyn/issues/5881/comments","events_url":"https://api.github.com/repos/dotnet/roslyn/issues/5881/events","html_url":"https://github.com/dotnet/roslyn/issues/5881","id":110916386,"number":5881,"title":"Proposal: 'using' local variable declaration","user":{"login":"rhencke","id":178348,"avatar_url":"https://avatars.githubusercontent.com/u/178348?v=3","gravatar_id":"","url":"https://api.github.com/users/rhencke","html_url":"https://github.com/rhencke","followers_url":"https://api.github.com/users/rhencke/followers","following_url":"https://api.github.com/users/rhencke/following{/other_user}","gists_url":"https://api.github.com/users/rhencke/gists{/gist_id}","starred_url":"https://api.github.com/users/rhencke/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rhencke/subscriptions","organizations_url":"https://api.github.com/users/rhencke/orgs","repos_url":"https://api.github.com/users/rhencke/repos","events_url":"https://api.github.com/users/rhencke/events{/privacy}","received_events_url":"https://api.github.com/users/rhencke/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dotnet/roslyn/labels/Area-Language%20Design","name":"Area-Language Design","color":"5319e7"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":41,"created_at":"2015-10-12T06:48:45Z","updated_at":"2015-10-14T04:39:40Z","closed_at":null,"body":"## Motivation\r\nIn C# (and VB), using statements contain an embedded statement (traditionally, a block statement) that contains the code that will be run before the resource is disposed.\r\n\r\nWhen multiple resources require disposal, using statements are nested to accomplish this (though traditionally written at the same level of indentation)\r\n\r\n```csharp\r\nusing(var r = new Resource()) \r\nusing(var s = new OtherResource())\r\n{\r\n}\r\n```\r\n\r\nThis works well if all resources can be acquired at the same time, but this cannot always be done.  The motivating example, in this case, is the traditional boilerplate for ADO.NET, in which the block scopes provide more noise than signal.\r\n\r\n```csharp\r\nvoid foo()\r\n{\r\n    using (var conn = new SqlConnection(\"...\"))\r\n    using (var cmd = conn.CreateCommand())\r\n    {\r\n        connection.Open();\r\n\r\n        using (var reader = cmd.ExecuteReader())\r\n        {\r\n            while (reader.Read())\r\n            {\r\n                // ...\r\n            }\r\n        }\r\n    }\r\n}\r\n```\r\n\r\n## Proposal\r\n\r\nFor scenarios like this, explicitly providing block scope often feels like overkill.  While it's important to dispose of the resources properly, the function scope surrounding the using is often sufficient enough.\r\n\r\nI would like to propose the following syntax:\r\n```using``` *local-variable-declaration* ```;```\r\n\r\nThis would effectively be syntactic sugar for 'Place this local variable declaration in a using statement, and place all statements within the same block that follow this statement into the statement list of that using block'.  Basically, the desired effect is the variable follows normal scoping rules, and as soon as the variable falls out of scope, the resource is disposed.\r\n\r\nWith this new syntax, we can now write the above example as:\r\n\r\n```csharp\r\nvoid foo()\r\n{\r\n    using var conn = new SqlConnection(\"...\");\r\n    using var cmd = conn.CreateCommand();\r\n    \r\n    connection.Open();\r\n\r\n    using var reader = cmd.ExecuteReader();\r\n\r\n    while (reader.Read())\r\n    {\r\n        // ...\r\n    }\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/comments/147933466","html_url":"https://github.com/dotnet/roslyn/issues/5881#issuecomment-147933466","issue_url":"https://api.github.com/repos/dotnet/roslyn/issues/5881","id":147933466,"user":{"login":"bondsbw","id":316704,"avatar_url":"https://avatars.githubusercontent.com/u/316704?v=3","gravatar_id":"","url":"https://api.github.com/users/bondsbw","html_url":"https://github.com/bondsbw","followers_url":"https://api.github.com/users/bondsbw/followers","following_url":"https://api.github.com/users/bondsbw/following{/other_user}","gists_url":"https://api.github.com/users/bondsbw/gists{/gist_id}","starred_url":"https://api.github.com/users/bondsbw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bondsbw/subscriptions","organizations_url":"https://api.github.com/users/bondsbw/orgs","repos_url":"https://api.github.com/users/bondsbw/repos","events_url":"https://api.github.com/users/bondsbw/events{/privacy}","received_events_url":"https://api.github.com/users/bondsbw/received_events","type":"User","site_admin":false},"created_at":"2015-10-14T04:39:40Z","updated_at":"2015-10-14T04:39:40Z","body":"@diryboy I get the difference between aggressive and suggestive, but typing ```using``` followed by <kbd>space</kbd> probably should not automatically type anything.  Perhaps ```using``` followed by <kbd>space</kbd><kbd>space</kbd> would cause autocomplete to kick in, but if you were going for ```using Abc = ...``` then you wouldn't type the second space that selects an item to autocomplete.  Only if you want ```using Abc Def = ...``` would you ever type ```using``` <kbd>space</kbd><kbd>space</kbd>, the second space selecting the top type in the autocomplete list.\r\n\r\nSo I think this is still unambiguous, and similarly other cases would be too (you wouldn't want ```foreach``` <kbd>(</kbd> to automatically select something, you would want that to happen upon ```foreach``` <kbd>(</kbd><kbd>space</kbd>)."}}