1

If I add an enum to the default code for a new c# Console App:

// See https://aka.ms/new-console-template for more information
enum testEnum
{
    one = 1,
    two = 2
}
Console.WriteLine("Hello, World!");

I get the following error on the Console.WriteLine line:
Error CS8803 Top-level statements must precede namespace and type declarations.

Moving the enum to the bottom of the code fixes the error. Is there a way to keep the enum at the top and get rid of the error?

Hal Heinrich
  • 544
  • 1
  • 4
  • 21
  • 1
    Not when using top-level statements, which is what the compiler error is trying to tell you. See https://stackoverflow.com/a/69601663/43846 – stuartd Dec 04 '21 at 21:44

1 Answers1

1

As @stuartd said, you cannot declare an enum before using statements such as Console.WriteLine(). stackoverflow.com/a/69601663/43846 explains that you can still use the old form of Program.cs and then declare the enum in Program class before calling Console.WriteLine() in main.

TarHalda
  • 1,050
  • 1
  • 9
  • 27