Your First C# Program
Why this matters: Up to now you have been installing things. From this lesson on, you are writing code. The first time your program asks you a question and replies with something based on your answer is a small but real shift. From here you are building, not just configuring.
Step one: Hello, World
The shortest complete C# program in .NET 8:
// This is a comment. The compiler ignores anything after //
Console.WriteLine("Hello, World!");
That one line:
- Uses
Console, a built-in class for printing to and reading from the terminal - Calls
WriteLineto print text followed by a newline - Ends with a semicolon, which every C# statement requires
Run it. You should see Hello, World! in your terminal.
Step two: ask the user a question
A program that talks back is more interesting than one that just announces itself:
Console.WriteLine("=== Welcome ===");
Console.Write("What is your name? ");
string name = Console.ReadLine();
Console.Write("Where are you from? ");
string city = Console.ReadLine();
Console.WriteLine();
Console.WriteLine($"Hello, {name} from {city}. Welcome to C#.");
New things in this program:
Console.Writeis likeWriteLinebut does not add a newline at the end. Use it for prompts when you want the cursor to stay on the same line.Console.ReadLine()waits for the user to type a line and press Enter, then hands you back what they typed as astring.string name = Console.ReadLine()declares a variable callednameand stores the user's input in it.$"..."is string interpolation. The{name}and{city}parts get replaced with the values of those variables when the string is built.
Run it. Answer the prompts. The output should feel like a real conversation.
Step three: a small VAT calculator
Now we add a bit of arithmetic. Enter a price, and the program adds 15% VAT.
Console.WriteLine("=== Price Calculator (with VAT) ===");
Console.Write("Enter price (excluding VAT): R");
string input = Console.ReadLine();
decimal price = decimal.Parse(input);
decimal vat = price * 0.15m;
decimal total = price + vat;
Console.WriteLine();
Console.WriteLine($"Price: R{price:F2}");
Console.WriteLine($"VAT (15%): R{vat:F2}");
Console.WriteLine($"Total: R{total:F2}");
New things in this program:
decimalis C#'s type for money. It is more accurate thandoublefor currency, which is why banks and accounting systems use it. Themat the end of0.15mtells the compiler "this is a decimal, not a double".decimal.Parse(input)converts the string the user typed (input is always a string) into adecimalnumber.:F2is a format specifier. It tells the formatter "show this number with exactly two decimal places". Try changing it to:F0and see what happens.
Run it with a value like 499. You should see:
Price: R499.00
VAT (15%): R74.85
Total: R573.85
Syntax rules you will hit immediately
These are the four rules that catch every beginner.
Semicolons end statements
Console.WriteLine("Hello"); // Correct
Console.WriteLine("Hello") // Error: missing semicolon
C# is case sensitive
name, Name and NAME are three different things. Built-in types like string are lowercase, classes you create are PascalCase.
string name = "Sipho"; // Correct
String name = "Sipho"; // Works, but the convention is lowercase
STRING name = "Sipho"; // Error: STRING does not exist
Strings use double quotes, characters use single quotes
string greeting = "Hello"; // String, double quotes
char letter = 'A'; // Single character, single quotes
string wrong = 'Hello'; // Error
Braces group code into blocks
You will meet braces properly in the next module, but for now know that { } groups statements that belong together:
if (age >= 18)
{
Console.WriteLine("You are an adult");
}
Comments
There are two kinds of comment you will write all the time:
// Single-line comment
/* Multi-line comment.
Useful for longer explanations
or for temporarily disabling a block of code. */
Use them sparingly. Good code reads almost like English; a comment is most useful for explaining why something is done, not what.
Running your program
Visual Studio
- F5: run with debugging
- Ctrl+F5: run without debugging (faster)
Command line
dotnet run
Four common beginner mistakes
| Mistake | What to do |
|---|---|
| Forgetting the semicolon | Read the error message. The compiler usually points at the next line, but the missing semicolon is on the line before. |
| Single quotes around a string | Use double quotes. Single quotes are only for single characters (char). |
Mistyping Console | Watch for "Consol", "console" (lowercase) or "Counsole". IntelliSense will usually catch this if you slow down. |
| Crashing when the user types something weird | decimal.Parse("abc") will throw an exception. You will learn how to handle this in Module 4 (error handling). |
Try it yourself
Write a small program that:
- Asks the user for their name
- Asks the user for their age
- Calculates what year they were born
- Displays a message like:
Hello Lerato, you were born in 1998.
Hint: to do arithmetic with the age, you need to parse it from a string to an int:
int age = int.Parse(Console.ReadLine());
And to get the current year:
int currentYear = DateTime.Now.Year;
What's next
In the next lesson we dive into how your C# code actually becomes a running program: the role of the compiler, the .NET runtime and what "IL" means.