
Introduction
Modern versions of C# and .NET introduced top-level statements, a feature that lets developers write a program without explicitly declaring a Program class or a Main method. In ASP.NET Core Web API projects, the default template now uses this minimal style, placing configuration, dependency injection, and middleware registration directly at the top of Program.cs. While this approach reduces boilerplate, many teams still prefer the traditional structure. That is why the Visual Studio project wizard includes an option labeled "Do not use top-level statements." In this article, we explain what that option does, why developers choose it for Web API projects, the trade-offs involved, and how to decide which style is right for your team.
How AAMAX.CO Builds Robust Web APIs and Applications
Choosing the right project structure is just one of many architectural decisions that influence how maintainable an API will be over time. AAMAX.CO is a full-service digital marketing company offering web development, digital marketing, and SEO services worldwide. Their development team designs and builds scalable backends, RESTful APIs, and integrations using modern frameworks and clean architecture principles. They help businesses decide on conventions, establish coding standards, and deliver APIs that are secure, well-documented, and easy for future developers to extend. If your organization needs a dependable backend for a web or mobile product, they can take the project from planning to deployment.
What Are Top-Level Statements?
Top-level statements were introduced in C# 9 to simplify entry points. Instead of writing a namespace, a static class, and a static void Main(string[] args) method, you can write executable code directly in a single file. The compiler automatically generates the class and Main method behind the scenes. Starting with .NET 6, most project templates, including ASP.NET Core Web API, adopted this style along with the minimal hosting model, which combines the old Startup.cs and Program.cs into a single concise file.
What the "Do Not Use Top-Level Statements" Option Does
When you create a new Web API project in Visual Studio or with the .NET CLI, you can opt out of top-level statements. In Visual Studio, this appears as a checkbox in the project creation dialog. With the CLI, you can use the --use-program-main flag. Selecting this option generates a Program.cs file with an explicit namespace, a Program class, and a Main method. The application still uses the modern minimal hosting APIs such as WebApplication.CreateBuilder; only the surrounding structure changes. This means you get familiar, explicit code without giving up the performance and simplicity improvements of newer .NET versions.
Why Developers Choose to Avoid Top-Level Statements
Familiarity and readability: Developers who learned C# before .NET 6 often find the explicit structure easier to read. Seeing a class and Main method makes the entry point obvious, especially for newcomers who may be confused about where execution begins.
Consistency across codebases: Large organizations frequently maintain many services created over several years. Using explicit entry points keeps new projects consistent with older ones, reducing cognitive load when switching between repositories.
Easier organization of helper methods: In top-level files, any methods you declare become local functions, and types must be placed after the statements. With an explicit class, you can add private static methods, constants, and nested logic in a conventional way.
Testing and tooling: Integration testing with WebApplicationFactory requires referencing the Program class. With top-level statements, the generated class is internal by default, so teams often add a public partial class Program {} declaration to expose it. Explicit entry points make this unnecessary and clearer.
The Case for Keeping Top-Level Statements
Top-level statements are not without merit. They reduce boilerplate, make small APIs and microservices extremely concise, and align with the minimal API style that Microsoft promotes. For tutorials, prototypes, and small services, the reduced ceremony helps developers focus on configuration and endpoints instead of structural code. Many modern teams embrace this style and organize complexity through extension methods that register services and middleware in separate files, keeping Program.cs short and readable.
Structuring a Maintainable Web API Either Way
Regardless of which style you choose, the key to a maintainable API is good organization. Move service registration into extension methods such as AddApplicationServices and AddInfrastructure. Group endpoints logically using controllers or minimal API route groups. Separate business logic into services or application layers rather than embedding it in endpoints. Configure options through strongly typed settings classes. Keep cross-cutting concerns like logging, validation, error handling, and authentication in dedicated middleware or filters. These practices matter far more than whether your entry point is explicit.
Teams building complex platforms that combine APIs, dashboards, and customer portals often benefit from professional web application development expertise to ensure the architecture scales gracefully as features are added.
Performance and Runtime Differences
There is no meaningful runtime difference between the two styles. Top-level statements are a compile-time convenience; the compiler generates an equivalent Main method. Your API's speed, memory usage, and startup time depend on its configuration, dependencies, and code quality, not on whether the entry point is written explicitly. The choice is purely about developer experience and team conventions.
Converting Between Styles
If you start with top-level statements and later decide to switch, conversion is simple. Wrap the existing code inside a namespace, a Program class, and a static void Main(string[] args) method, or static async Task Main if you use await. Move any local functions into private static methods. Converting in the other direction is equally straightforward: remove the wrapping class and method, keeping the statements in order. Because the change is mechanical, you are never locked into one approach.
Making the Right Choice for Your Team
Consider your team's experience, the size of the project, and your existing conventions. If your developers are comfortable with modern C# and you value concise code, top-level statements are a fine default. If your organization maintains many legacy services, onboards developers from varied backgrounds, or prefers explicit structure, selecting "Do not use top-level statements" offers clarity and consistency. Document whichever choice you make in your coding guidelines so that every new project follows the same pattern.
Conclusion
The "Do not use top-level statements" option in ASP.NET Core Web API templates gives developers control over how their application's entry point is written. It produces an explicit Program class and Main method while preserving all the benefits of the modern hosting model. Neither style is inherently better; the decision hinges on readability preferences, team consistency, and testing needs. Focus on clean architecture, sensible organization, and strong conventions, and your Web API will remain maintainable no matter which entry-point style you adopt.
