Top-level statements must precede namespace and type declarations

A few days ago I wanted to create a simple C# Console Application. And faced with the error: “error cs8803: Top-level statements must precede namespace and type declarations“. My problem was that I started to add types to the Program.cs file:
Top-level statements must precede namespace and type declarations

After I moved the Person class declaration into a separate file error disappeared:
fix Top-level statements must precede namespace and type declarations error

Thus my problem was in mixing top-level statements and non-TLS.

Used artifacts:

  • Visual Studio 2022
  • Console Application .NET 6

Leave a Comment