When to use vs traditional Main
Context: Choose top-level statements for simple console apps and learning; use traditional Main for complex applications. This guidance applies to .NET 10.
Choose between top-level statements and traditional Main based on your application’s complexity and purpose.
Use Top-Level Statements When
Section titled “Use Top-Level Statements When”- Simple console apps: Quick scripts, utilities, demos
- Learning C#: Less confusing for beginners
- Small tools: One-file programs
- Prototyping: Rapid iteration
Use Traditional Main When
Section titled “Use Traditional Main When”- Multiple entry points: Need several
Mainmethods - Complex initialization: Lots of setup code before app logic
- Team conventions: Existing codebase uses traditional style
- Advanced scenarios: Customizing the entry point signature
Example Comparison
Section titled “Example Comparison”Top-level (good for simple apps):
var config = LoadConfig();var app = new MyApp(config);await app.RunAsync();Traditional (better for complex apps):
public class Program{ public static async Task Main(string[] args) { try { var config = LoadConfig(args); await InitializeLogging(config); var app = new MyApp(config); await app.RunAsync(); } catch (Exception ex) { Log.Error(ex); Environment.Exit(1); } }}