Your First Program
Since .NET 10, a C# program can be a single file that you run directly — no project needed. In this lesson you'll write one, run it, and learn what each part does, how to print text, and how to read the compiler's error messages.
Hello, World!
Create a folder for your C# experiments, and inside it a file named hello.cs containing one line:
Console.WriteLine("Hello, World!");
Open a terminal in that folder and run it:
dotnet run hello.cs
Hello, World!
The first run takes a few seconds because .NET builds the program behind the scenes; later runs reuse the build and start almost instantly. The shorter form dotnet hello.cs does the same thing.
What the line means
Consoleis a class from the base class library that represents the terminal window..WriteLineis a method ofConsole: an action it can perform. It writes text followed by a line break.("Hello, World!")passes an argument to the method — here a string, text in double quotes.;ends the statement. Every statement in C# finishes with a semicolon.
Statements written directly in the file like this are called top-level statements. The compiler wraps them in a hidden entry point that runs from top to bottom. Larger programs often use an explicit Main method instead, which you'll see in the next lesson.
Several statements
Statements run one after another. Console.Write prints without a line break, so the next output continues on the same line:
Console.WriteLine("C# runs on .NET."); Console.Write("Same "); Console.Write("line, "); Console.WriteLine("then a newline."); Console.WriteLine(); Console.WriteLine("After an empty line.");
C# runs on .NET. Same line, then a newline. After an empty line.
Console.WriteLine() with no argument just prints an empty line.
Printing numbers and calculations
Numbers don't need quotes, and C# works out arithmetic before printing it. You can also join text and values with +, or put them inside an interpolated string that starts with $ — values in { } are filled in:
Console.WriteLine(42); Console.WriteLine(7 * 6); Console.WriteLine("7 * 6 = " + 7 * 6); Console.WriteLine($"A week has {7 * 24} hours.");
42 42 7 * 6 = 42 A week has 168 hours.
Special characters
Inside a string, a backslash starts an escape sequence for characters you can't type directly:
Console.WriteLine("Line one\nLine two"); Console.WriteLine("Name\tScore"); Console.WriteLine("She said \"hi\"."); Console.WriteLine("C:\\Users\\sam");
Line one Line two Name Score She said "hi". C:\Users\sam
| Escape | Meaning |
|---|---|
\n | New line |
\t | Tab |
\" | Double quote |
\\ | Backslash |
Lesson 16 shows verbatim and raw strings, which make paths and multi-line text easier.
When something goes wrong
If the code has a mistake, the compiler refuses to run it and reports where the problem is. Forgetting the semicolon:
Console.WriteLine("Hello")
Program.cs(1,27): error CS1002: ; expected
Read the message as file(line,column): error code: description. Line 1, column 27 is just after the closing parenthesis, where the semicolon should go. Every error has a code such as CS1002 that you can search for online.
C# is also case-sensitive: console and Console are different names.
console.WriteLine("Hello");
Program.cs(1,1): error CS0103: The name 'console' does not exist in the current context
Running a file like a script
On macOS and Linux you can make a C# file directly executable. Add a "shebang" line at the very top, then mark the file as executable:
#!/usr/bin/env dotnet Console.WriteLine("Hello from a C# script!");
chmod +x greet.cs ./greet.cs
Hello from a C# script!
Try it yourself
Change the message, add a line that prints your name, and print the number of minutes in a day.
Hello, World! I'm learning C#. Minutes in a day: 1440