Every great skyscraper has a defined structure — a foundation, floors, rooms, and a roof. A C# program is exactly the same! Let's dissect each "floor" of a C# program and understand what it does and why it's there.
using Directives (The Supply Trucks)Before construction begins, supply trucks arrive with materials. In C#, using directives import namespaces — collections of pre-built tools and classes.
Without using System;, you'd have to write System.Console.WriteLine() every single time. The using directive is like telling the delivery crew: "Just leave the System tools right here where I can reach them."
namespace (The Building's Address)A namespace is your building's address. It organizes your code and prevents naming conflicts. Imagine two architects both name their class "Calculator" — namespaces keep them separate!
Real-world namespaces look like: Microsoft.AspNetCore.Mvc, Unity.Engine, MyCompany.ProjectName.Feature. They use dots to create a hierarchy, like a postal address: Country → City → Street.
class (The Blueprint)A class is the blueprint for a building. It defines what the building contains and what it can do. Every piece of C# code lives inside a class.
The name Program is just a convention for the main class. You could call it Skyscraper or CityBuilder — C# doesn't care about the name, only that it contains the Main method.
Main Method (The Front Door)The Main method is the front door of your program. When someone runs your application, the CLR (Common Language Runtime) looks for this exact method to start execution.
Let's decode each keyword:
static — This method belongs to the class itself, not to an instance. The runtime can call it without creating an object first.void — This method doesn't return any value. (You can also use int to return an exit code, or async Task for async programs.)Main — The special name the CLR looks for. Must be capital M!string[] args — An optional array of command-line arguments. When you run myapp.exe hello world, args contains ["hello", "world"]. You can omit this parameter if you don't need it.Here's how all the floors stack together into one beautiful skyscraper:
💡 Pro tip: In modern C# (version 9+), you can use top-level statements — writing code without the class and Main boilerplate. But as an architect, you should know the full structure first before taking shortcuts!