compiler-explorer icon indicating copy to clipboard operation
compiler-explorer copied to clipboard

[REQUEST]: C# compiler should default to executable when "Execute the code" is checked so that I can use top level statements

Open Neme12 opened this issue 1 year ago • 1 comments

Is your feature request related to a problem? Please describe

When I try using top-level statements in C# using .NET (main):

using System;

Console.WriteLine("Hello World");

I get error CS8805: Program using top-level statements must be an executable.

Describe the solution you'd like

It would be nice if the default was to use -target:exe when "Execute the code" is checked so that I could write simple programs like that and run them (and it would be even nicer if this was the default program that appears when selecting C#, without the class and Main method, to modernize the default code).

I don't really understand how this works because if I have a class and Main method, I can run the program just fine, as if the output type was set to an executable after all? Or maybe the compiler is invoked twice, once to just compile it, without the output type being set, and afterwards in order to run it compiled again, this time correctly setting the output type?

Describe alternatives you've considered

The alternative is as it is today (having to add a class and Main method), I guess?

Additional context

Not applicable

Neme12 avatar Feb 23 '24 20:02 Neme12

You can check the "Link to binary" in output options. By default it compiles code in library mode.

hez2010 avatar Feb 25 '24 06:02 hez2010