IDisposable objects can be declared with using var as top-level statement, or in interactive shell.
Current behavior
C# compiler throws error out. When the same statement is instead compiled with dotnet run, there is no error.
Sample shell output
!yuntian@Yuntian /home/yuntian> using var f = File.Open("/tmp/test", FileMode.OpenOrCreate);
(1,11): error CS1002: ; expected
!yuntian@Yuntian /home/yuntian> if (0 == 0) { using var f = File.Open("/tmp/test", FileMode.OpenOrCreate); }
!yuntian@Yuntian /home/yuntian> var f = File.Open("/tmp/test", FileMode.OpenOrCreate);
!yuntian@Yuntian /home/yuntian> f.Dispose()
!yuntian@Yuntian /home/yuntian>
Reasoning
Although using var is not exactly useful in main() or in interactive shell where variables will be discarded anyway when the process ends, scripts can be reworked into functions and reused elsewhere. Allowing the variable to be properly declared can save debugging effort down the line.
Expected behavior
IDisposable objects can be declared with
using var
as top-level statement, or in interactive shell.Current behavior
C# compiler throws error out. When the same statement is instead compiled with
dotnet run
, there is no error.Sample shell output
Reasoning
Although
using var
is not exactly useful in main() or in interactive shell where variables will be discarded anyway when the process ends, scripts can be reworked into functions and reused elsewhere. Allowing the variable to be properly declared can save debugging effort down the line.