Your first C# program
Make a console project with dotnet new console -o Hello, then grow Program.cs into five lines: a comment and four statements. Watch dotnet run build the code into Hello.dll and run it line by line: WriteLine prints a line and moves the cursor down, Write stays on the line, ReadLine waits for your typing and keeps it in a variable, and an interpolated string puts that value into a greeting.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01The program runs Console.Write("A"); then Console.WriteLine("B"); then Console.Write("C");. What does the terminal show?
Write prints its text and no line break, so B follows A on the same line. WriteLine prints B and then a line break, so C starts a new line.
02You forget the semicolon: Console.WriteLine("Hello, World!") . What happens when you type dotnet run?
dotnet run builds first. The compiler checks every line before anything runs, and a statement without its semicolon is error CS1002, so no program is made.
03What does the line // My first C# program do when the program runs?
Everything from // to the end of the line is a comment: a note for people. The compiler ignores it, so it never changes what the program does.
04After var name = Console.ReadLine(); you type Ada and press Enter. What does Console.WriteLine($"Hi, {name}!"); print?
The $ makes it an interpolated string: each {name} is replaced by the current value of the variable name, which ReadLine set to the line you typed.
05What does dotnet new console -o Hello create?
It makes a project: the folder Hello holding the project file Hello.csproj and Program.cs. Hello.dll is only made later, when the project is built.
Glossary
- Statement
- One instruction in a program, such as Console.WriteLine("Hi");. Most statements end with a semicolon, and they run in order.
- Comment
- Text after // (to the end of the line) that the compiler ignores: a note for people reading the code.
- String literal
- Fixed text written in the code inside double quotes, such as "Hello, World!".
- Method
- A named action you can call, such as Console.WriteLine. You call it with brackets and pass it values inside them.
- Console.Write / WriteLine
- Write prints text and leaves the cursor on the same line; WriteLine prints text and then a line break.
- Variable
- A named box in memory that holds a value, such as name. var lets the compiler work out its type.
- Interpolated string
- A string starting with $, in which each {name} is replaced by that variable's value.
- Project file (.csproj)
- The file dotnet new makes next to your code; it holds the settings the build uses, such as the .NET version.
Notes and sources
- In .NET 6 and later, dotnet new console -o Hello writes a Program.cs with two lines: a comment linking to aka.ms/new-console-template and the statement Console.WriteLine("Hello, World!");.
- The letters you type are shown by the terminal, not by the program (the terminal echoes them). If the input came from a file instead, the program's own output would be Hello, World!, then Your name? Hi, Ada! on one line.
- WriteLine ends the line with Environment.NewLine: a line feed (\n) on macOS and Linux, a carriage return and line feed (\r\n) on Windows.
- ReadLine returns null when there is no more input (Ctrl+Z then Enter on Windows, Ctrl+D on macOS and Linux). That is why its type is string?, a string that may be null. With var, name gets that type, and {name} prints nothing for null.
- Top-level statements with no return statement end with exit code 0. The build writes Hello.dll into a folder such as bin/Debug/net8.0 (the last part names the .NET version).
- Console works without a "using System;" line because new console projects turn on implicit usings (.NET 6 and later).
- The program is real C#. Put the five lines in Program.cs, type dotnet run, enter Ada, and it prints Hi, Ada!.
- Microsoft Learn: Hello World (C# tutorial)
- Microsoft Learn: Create a .NET console application
- Microsoft Learn: dotnet new
- Microsoft Learn: dotnet run
- Microsoft Learn: The console app template generates top-level statements
- Microsoft Learn: Top-level statements
- Microsoft Learn: Console.WriteLine method
- Microsoft Learn: Console.ReadLine method
- Microsoft Learn: String interpolation using $
- Microsoft Learn: Compiler Error CS1002
- Microsoft Learn: Comments (C# reference)
Quiz Unlocks after the last step.