Your first console app and top-level statements
Course contents (16 lessons)
- Getting started
- Installing the .NET 10 SDK and checking it works
- Your first console app and top-level statements
- The building blocks
- Variables and basic types
- Working with strings and interpolation
- Reading input and writing output
- Making decisions and repeating work
- Conditionals and switch expressions
- Loops: for, while, do and foreach
- Collections and methods
- Arrays and List of T
- Writing your own methods
- Objects and classes
- Classes and objects
- Properties, encapsulation and object initialisers
- Handling null safely
- Errors and files
- Exceptions: try, catch, finally and throw
- Reading and writing files
- Final project: expense tracker
- Expense tracker, part 1: planning, model and storage
- Expense tracker, part 2: the menu and summaries
Creating the project
Open a terminal in your practice folder and run these three commands:
dotnet new console -n HelloWorld
cd HelloWorld
dotnet run
The first command creates a folder called HelloWorld with a ready-to-run console project in it. The second moves into it. The third compiles the code and runs it. You should see Hello, World! in the terminal. That is a working C# program, and you did not write a line yet.
What is in the folder
Look inside the folder. You will see two files and, after the first run, two folders.
Program.csis your code. This is the file you will edit most of the time.HelloWorld.csprojis the project file. It is a small XML file that tells the SDK what kind of app this is and which .NET version it targets.binholds the compiled output.objholds temporary build files.
You never edit bin or obj. You can delete them at any time. The next build recreates them. Open the project file once so it is not a mystery:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Exe means the project builds a program you can run. net10.0 is the target version. ImplicitUsings adds common using lines for you, so basic types are available without setup. Nullable turns on null safety warnings. We will use that in a later lesson.
Top-level statements
Open Program.cs. In older tutorials you will see a class called Program, a method called Main, and a namespace, all before the first useful line. Modern C# lets you skip all of that. The file contains only your statements, and the compiler wraps them in a Main method for you. These are called top-level statements.
Replace the file's content with this:
Console.WriteLine("Hello from C#!");
string name = "Murtaza";
Console.WriteLine("My name is " + name);
Console.WriteLine("2 + 3 = " + (2 + 3));
Save and run dotnet run again.
Walking through the code
Console is a class from .NET. It represents the terminal window. WriteLine is one of its methods. It writes text and then moves to a new line. The text goes between double quotes, and that quoted text is called a string. Every statement ends with a semicolon. The compiler uses the semicolon to know where one instruction stops.
The second line creates a variable. We will cover variables properly in the next lesson. For now, read it as "make a box named name that holds the text Murtaza".
The last line shows that C# does the sum inside the brackets first, then joins the result to the text. Try removing the brackets. You will get 2 + 3 = 23, because C# then joins the 2 and the 3 as text. That is a good example of how exact the language is.
What happens when you run
dotnet run does two jobs. It builds the project, which means the compiler turns your C# into instructions the runtime understands. Then it starts the program. If the build finds an error, it prints the file name, the line number and a message, and the program does not start. Read the first error and fix that one only. Later errors are often caused by the first.
Common mistakes
- Missing semicolon. The error message points at the line after the real problem. Check the line above.
- Wrong case. C# is case-sensitive.
console.writelinedoes not work. It must beConsole.WriteLine. - Running the command in the wrong folder.
dotnet runneeds to be in the folder with the.csprojfile, or you must pass--project. - Single quotes for text. Single quotes are for one character. Text needs double quotes.
Try it yourself
- Change the program so it prints your own name and the city you live in on two separate lines.
- Print the result of
10 * 7 - 4. Predict the answer before you run it. - Deliberately delete one semicolon, run the program, and read the error message. Put the semicolon back.
- Delete the
binandobjfolders and run again. Confirm they come back.
Console.WriteLine("Hello from C#!");
string name = "Murtaza";
Console.WriteLine("My name is " + name);
Console.WriteLine("2 + 3 = " + (2 + 3));
This course is free to read. Create a free account to enrol, save your progress and pick up where you left off.
Sign up free I already have an account