Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application crash when using empty statement (semicolon) in Startup.cs #6409

Closed
leonkleiboerstrukton opened this issue Jun 24, 2021 · 2 comments

Comments

@leonkleiboerstrukton
Copy link

Description

I was trying to make an API in Visual Studio, using net 5.0.7 and the APS.NET Core Web API template.
My application keeps crashing, before even hitting the first line of code in Program.cs In the output window, the only information shown was:

'xxx.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\5.0.7\System.Runtime.dll'.
The program '[0x254C] xxx.exe: Program Trace' has exited with code 0 (0x0).
The program '[0x254C] xxx.exe' has exited with code 0 (0x0).

I couldn't figure out was wrong, so I started from scratch to pin point the issue. Turns out that I had a semicolon in Startup.cs after my usings like this:

using System;
using System.IO.Compression;
using System.Runtime.InteropServices;
;

When removing the semicolon on the last line, the application worked as expected.
I know I have a compiler warning, 'Remove this empty statement', and I know I should have solved it, but I didn't expect the application to crash like this. IMHO, the compiler should have raised an error, or the application should have raised a nice descriptive exception message.

Configuration

Tested in on Windows 10 and on Fedora Linux 33, in both cases with the same result.

Regression?

Do not know, tested it with net 5.0.5 as well on Windows, same result.

Other information

none

@svick
Copy link
Contributor

svick commented Jun 24, 2021

The problem is that the application is not crashing, it's doing exactly what you told it to do, which is nothing. C# 9 introduced top-level statements, which means you can have statements outside of a class and those take priority over Program.Main. So your program executes the empty statement and then immediately exits.

But this specific case is clearly confusing, so it will become an error in a future version of C#, see dotnet/roslyn#53472.

@leonkleiboerstrukton
Copy link
Author

thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants