Your First Program
By the end of this lesson
Write, build and run a small program, and read its output and its errors.
Time to run something. The goal of this lesson is not the program itself — it is confirming your setup works and that you can complete the write, build, run loop you will use thousands of times.
You need the .NET SDK installed. Then, from a terminal:
Create a project
Run: dotnet new console -o FirstProgram. This creates a folder with a minimal working application already in it.
Move into it
Run: cd FirstProgram
Run it
Run: dotnet run. The SDK compiles your code and starts the program. You should see a line of output.
string name = "Anvi";
int year = 2026;
Console.WriteLine($"Hello from {name}.");
Console.WriteLine($"This program ran in {year}.");- Line 1 stores text under the name name. Text values are wrapped in double quotes.
- Line 2 stores a whole number under the name year. Numbers need no quotes.
- Lines 4 and 5 write output. The $ before the quote allows {name} and {year} to be replaced by their values.
Run it again with dotnet run. You should see both lines. If you do, your environment works and everything else in this course is now possible.
Now break it on purpose
This part matters more than the working version. Errors are going to be a constant companion, so it is worth meeting one while you know exactly what caused it.
Change {name} to {nmae} and run again. The build fails, and the message names the problem and the line. Read it before fixing it. Getting comfortable with error messages now saves a great deal of frustration later.
Summary
- dotnet new console creates a project; dotnet run builds and starts it
- The $ prefix on a string lets you insert values with {braces}
- Break your code deliberately once, so error messages become familiar rather than alarming
- Fix the first reported error first — later ones are often consequences of it
Practice
Attempt each one before opening the solution. Getting it wrong first is how the idea sticks.
Try it yourself
Try it yourself
Extend the program so it stores your town and the number of programming languages you have used (zero is a perfectly good answer), then prints one sentence containing both.
Show solution
Two more variables and one more output line. Note that the number needs no quotes while the text does.
string town = "Pune";
int languagesUsed = 0;
Console.WriteLine($"I am in {town} and I have used {languagesUsed} languages so far.");Challenge
Challenge
Make the program ask for the user's name and then greet them by it. You will need Console.ReadLine(), which waits for typed input and gives back what was entered.
Then consider: what happens if the user types nothing and just presses Enter?
Show solution
Console.ReadLine() returns the text that was typed. If the user enters nothing, you get an empty string, so the greeting looks unfinished.
Handling that case is a decision you now have to make consciously — which is exactly the kind of thinking the first lesson described.
Console.Write("What is your name? ");
string? name = Console.ReadLine();
if (string.IsNullOrWhiteSpace(name))
{
Console.WriteLine("Hello, stranger.");
}
else
{
Console.WriteLine($"Hello, {name}.");
}Saved in this browser only.