First program and Console.WriteLine
Your first C# program
Every C# program starts executing from the Main method. Here is the smallest working program:
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}
Reading the code
using System;— imports the namespace that contains theConsoleclass.class Program— in C#, all code lives inside classes.static void Main()— the entry point: execution begins here.Console.WriteLine(...)— prints text and moves to a new line.
WriteLine or Write?
Console.Write("Hello, ");
Console.WriteLine("world!"); // output: Hello, world!
Write keeps the cursor on the same line, WriteLine moves it to the next one.
The most common mistakes
- A missing
;at the end of a line — the compiler usually points at the line after the missing semicolon. - Wrong casing:
console.writelineis notConsole.WriteLine— C# is case-sensitive. - Missing quotes around text.
Practice
Open the Playground, print your own name, and try both Write and WriteLine. Then solve the first task in the course: "Hello, World".