Skip to main content

Classic Program Structure

Newer .NET templates hide the boilerplate using "top-level statements". To see the full, classic structure, tick Do not use top-level statements when creating the project.

Visual Studio project setup with "Do not use top-level statements" checked

If you are using VSCode, run dotnet new console --use-program-main

Syntax​

Your new project contains:

namespace MyApp;

class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
  • namespace MyApp groups related code and prevents naming conflicts
  • class Program: C# is object-oriented, so methods must live inside a type such as a class.
  • static void Main(...) is the entry point, where execution starts.
  • string[] args holds any command-line arguments passed when the program starts.

Everything you've written so far goes inside class Program! You'll learn more about classes in P2!