Compiler Error CS8802

Only one compilation unit can have top-level statements.

This error indicates that there are two or more top-level statements in a single compilation unit (single project or a single group of files compiled into a single binary file).

Example

The following sample of single compilation unit generates CS8802:

<!-- SingleCompilationUnit.csproj -->
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
  </PropertyGroup>
</Project>
// EntryFile.cs

int a = 0;
// SecondaryEntryFile.cs

int b = 1;    // CS8802: The top level statement already exists in EntryFile.cs

To correct this error

Use only one top-level statement in the project.
Top-level statements acts as an entry point to the program, so only one file may have top-level statement. All other statements must be defined as members of classes or structs.

See also