Skip to content

No explicit Main method

Context: With top-level statements, the compiler generates the Main method automatically. This works in .NET 10.

With top-level statements, the compiler automatically generates the Main method entry point. You don’t need to write it explicitly.

The C# compiler wraps your top-level code into a generated Main method. The code you write becomes the body of that method.

// This is all you need!
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine($"Hello {name}!");
using System;
using System.Threading.Tasks;
namespace MyApp
{
internal class Program
{
private static void Main(string[] args)
{
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine($"Hello {name}!");
}
}
}
// Async top-level statements
await Task.Delay(1000);
Console.WriteLine("Done!");