Tutorials / C# for Beginners / Your first console app and top-level statements

Your first console app and top-level statements

12 min read csharp
Course contents (16 lessons)
  1. Getting started
  2. Installing the .NET 10 SDK and checking it works
  3. Your first console app and top-level statements
  4. The building blocks
  5. Variables and basic types
  6. Working with strings and interpolation
  7. Reading input and writing output
  8. Making decisions and repeating work
  9. Conditionals and switch expressions
  10. Loops: for, while, do and foreach
  11. Collections and methods
  12. Arrays and List of T
  13. Writing your own methods
  14. Objects and classes
  15. Classes and objects
  16. Properties, encapsulation and object initialisers
  17. Handling null safely
  18. Errors and files
  19. Exceptions: try, catch, finally and throw
  20. Reading and writing files
  21. Final project: expense tracker
  22. Expense tracker, part 1: planning, model and storage
  23. 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.cs is your code. This is the file you will edit most of the time.
  • HelloWorld.csproj is 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.
  • bin holds the compiled output.
  • obj holds 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.writeline does not work. It must be Console.WriteLine.
  • Running the command in the wrong folder. dotnet run needs to be in the folder with the .csproj file, or you must pass --project.
  • Single quotes for text. Single quotes are for one character. Text needs double quotes.

Try it yourself

  1. Change the program so it prints your own name and the city you live in on two separate lines.
  2. Print the result of 10 * 7 - 4. Predict the answer before you run it.
  3. Deliberately delete one semicolon, run the program, and read the error message. Put the semicolon back.
  4. Delete the bin and obj folders and run again. Confirm they come back.
csharp
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