← Open in the full interactive course (progress tracking, search & more)

Write, compile, and run your very first C# program — and understand every step along the way.

You've learned what C# and .NET are. Now it's time to build something real.

In this lesson, you'll create a working C# application from scratch. You'll write code, compile it, and run it. You'll see the entire lifecycle of a C# program — from a blank file to a running application.

Think of this as the "Hello, World!" moment every developer remembers. It's small, but it's the foundation for everything that follows.

What Is a C# Application?

The Simple Explanation

A C# application is a program you write in the C# language that runs on the .NET platform. It's a set of instructions that tells the computer what to do — from printing text to handling complex business logic.

The Technical Definition

A C# application consists of one or more source code files (with a .cs extension) that are compiled into an assembly (a .dll or .exe file) which the .NET runtime executes. The simplest C# application is a console application — a program that runs in a terminal window.

Why Does It Exist?

The Problem

Before you can build anything useful, you need to know how to create any program at all. Many beginners get stuck at the very first step — writing code and seeing it run.

The Solution

Creating a "first application" solves all of these problems. It gives you a working template you can understand, modify, and extend. It's the starting point for every program you'll ever write.

The key insight

Every complex application starts as a simple one. Your first program is a blueprint for every program that follows.

Big Picture

Here's the journey your code takes — from your keyboard to a running application:

Write Code Compile (dotnet build) ▶ Run (dotnet run) See Output
A four-step journey from idea to running program

How It Works — Step by Step

Let's walk through the entire process of creating and running your first C# application. We'll use the .NET CLI (Command Line Interface) — the tool every .NET developer uses.

Step 1 — Install .NET SDK

First, you need the .NET SDK (Software Development Kit). This includes:

Download it from dotnet.microsoft.com/download and install it. After installation, open a terminal and verify:

$ dotnet --version
10.0.100

Step 2 — Create a New Project

In your terminal, navigate to where you want to store your projects. Then run:

$ dotnet new console -n MyFirstApp
The template "Console App" was created successfully.

This command creates a new console application named MyFirstApp. Let's look at what it created:

MyFirstApp/
obj/ ← build outputs (ignore for now)
Program.cs your code!
MyFirstApp.csproj ← project configuration
Tip: The obj/ folder contains temporary build files. You don't need to touch it. The .csproj file defines your project's settings, and Program.cs is where you write your code.

Step 3 — Open and Understand the Code

Open Program.cs in any text editor or IDE. You'll see this code:

        // See https://aka.ms/new-console-template for more information
        Console.WriteLine("Hello, World!");
    

That's it. Just one line of code. Let's break it down:

This single line tells .NET: "Write this text to the console."

.NET 10 / C# 14 note: The template uses top-level statements — a modern C# feature that removes boilerplate code. In older versions, you'd see a Main method and a class. With top-level statements, the compiler handles that for you automatically. Your code is cleaner and easier to read.

Step 4 — Run the Application

In the terminal, make sure you're inside the MyFirstApp/ folder. Then run:

$ dotnet run
Hello, World!

Congratulations! You've just written and run your first C# application.

Under the Hood

Let's trace exactly what happens when you run dotnet run. This is the execution flow:

THE COMPLETE EXECUTION FLOW
1. YOUR CODE (Program.cs)
Console.WriteLine("Hello, World!");
2. dotnet build — COMPILE TO IL
                IL_0000: ldstr "Hello, World!"
                IL_0005: call void [System.Console]System.Console::WriteLine(string)
                IL_000A: ret
            
3. dotnet run — LAUNCH THE CLR
4. JIT COMPILATION — IL → NATIVE CODE
5. EXECUTION
6. OUTPUT
Hello, World!

What's in the .csproj File?

The MyFirstApp.csproj file tells .NET how to build your project. With .NET 10, it's very simple:

        <Project Sdk="Microsoft.NET.Sdk">
          <PropertyGroup>
            <OutputType>Exe</OutputType>
            <TargetFramework>net10.0</TargetFramework>
            <ImplicitUsings>enable</ImplicitUsings>
            <Nullable>enable</Nullable>
          </PropertyGroup>
        </Project>
    

Simple Example — Make It Yours

Now let's change the code. Update Program.cs to say something more personal:

        Console.WriteLine("Hello, C# learner!");
        Console.WriteLine("Welcome to .NET 10.");
    

Run it again:

$ dotnet run
Hello, C# learner!
Welcome to .NET 10.

You've just made your first change! This is how all C# development works:

Real‑World Example

You might think: "But real applications don't just print text."

You're right. But the pattern is exactly the same. Here's how the "Hello, World" pattern scales:

Your First App

Console.WriteLine("Hello!");

→ Prints one line

Real Banking App

var balance = _accountService.GetBalance(userId);
Console.WriteLine($"Balance: {balance:C}");

→ Fetches data, then displays it

The structure is the same: get data → display output. Your first app is the foundation for every app you'll build.

Analogy

Your first C# application is like learning to turn on a light.

It's simple, but without this basic action, nothing else can work. Every complex system starts with a single action.

Common Confusion

dotnet run vs dotnet build

Command What It Does When To Use
dotnet buildCompiles code to ILCheck for compile errors, CI/CD
dotnet runBuilds and runs the appLocal development, testing changes

dotnet run does both — it builds and runs. It's your go‑to for development.

Common Mistakes

Mistake 1 — Not running from the right folder

Wrong: Running dotnet run from the wrong folder
Correct: Navigate to the folder containing .csproj first

# Wrong — this won't work
$ cd ~/projects
$ dotnet run
Couldn't find a project to run.
# Correct — navigate to project folder
$ cd ~/projects/MyFirstApp
$ dotnet run
Hello, World!

Mistake 2 — Forgetting to save the file

Wrong: Edit code, don't save, run → old output
Correct: Save (Ctrl+S) before running

Mistake 3 — Confusing WriteLine with Write

Console.Write("Hello");
Console.Write("World");

→ Output: HelloWorld (no new line)

Console.WriteLine("Hello");
Console.WriteLine("World");

→ Output: Hello (new line) World

When Should I Use This?

Use this pattern whenever you:

When you might use something different:

The console app is your foundation — you'll always come back to it.

Mental Model

dotnet new console = Create the blueprint
Program.cs = The instructions
dotnet run = Build and follow the instructions
Output = The result of following the instructions

Remember: Every program is just a set of instructions. Your first app is the simplest set of instructions that actually does something.

Key Takeaway


Check Your Understanding

You've created your first C# application, seen the full compilation and execution pipeline, and learned the essential .NET CLI commands. Now let's make sure you can apply this knowledge.

1. You've just written your first C# program. Which command should you use to compile and run it in one step?

Show answer

Correct: C

Why C is correct: dotnet run both builds (compiles) the project and executes it. It's the primary command for local development and testing.

Why A is incorrect: dotnet build only compiles the code — it does not run the application. It's useful for checking compilation errors or in CI/CD pipelines.

Why B is incorrect: dotnet compile is not a valid .NET CLI command. The actual compilation command is dotnet build.

Why D is incorrect: dotnet start is not a valid .NET CLI command. The correct command for running is dotnet run.

Reinforcement: dotnet run is your daily driver during development. It handles both compilation and execution, giving you the fastest feedback loop when you're writing code.

2. What does the following code do?

Console.WriteLine("Hello");
Console.Write("World");
Show answer

Correct: C

Why C is correct: WriteLine prints the text and adds a new line after it, so "Hello" appears on its own line. Write prints text without adding a new line, so "World" appears immediately after "Hello" on the same line. The output is:

HelloWorld

Why A is incorrect: This would be the output of Console.Write("Hello"); Console.Write("World"); — but that's not what the code does.

Why B is incorrect: This would be the output of Console.WriteLine("Hello"); Console.WriteLine("World"); — but the second line uses Write, not WriteLine.

Why D is incorrect: Neither Write nor WriteLine adds a space automatically. The output is HelloWorld without a space.

Reinforcement: Understanding the difference between Write and WriteLine is essential for controlling output formatting. WriteLine is used 90% of the time, but Write is useful when you need precise control over where text appears.

3. You run dotnet run and see this error: "Couldn't find a project to run." What is the most likely cause?

Show answer

Correct: B

Why B is correct: The dotnet command looks for a project file (.csproj) in the current folder. If it can't find one, it shows this error. You need to cd into the folder containing your .csproj file before running dotnet run.

Why A is incorrect: A syntax error would produce a compilation error with line numbers, not "Couldn't find a project to run." The error messages are different.

Why C is incorrect: If .NET weren't installed, you'd see something like 'dotnet' is not recognized — a completely different error.

Why D is incorrect: An empty Program.cs file would still be valid code (it just wouldn't do anything) and the project would still be found and run.

Reinforcement: Always check your current directory when working with the .NET CLI. The dotnet commands are context‑sensitive — they operate on the project in the current folder.

4. Place these .NET CLI commands in the correct order for a typical development workflow when you're starting a new project:

Show answer

Correct: A

Why A is correct: The correct workflow is:

  1. Create the project with dotnet new console
  2. Write/Edit the code in Program.cs
  3. Run the application with dotnet run

This is the natural order — create first, then edit, then run.

Why B is incorrect: You can't run a project that hasn't been created yet. dotnet run must come after dotnet new.

Why C is incorrect: You can't edit code before creating the project — there's nothing to edit. The project structure needs to exist first.

Why D is incorrect: While dotnet build is part of the workflow, it's not the first step. You need to create the project first. Also, dotnet run includes dotnet build automatically, so you typically don't need to run dotnet build separately during development.

Reinforcement: The development workflow is: Create → Edit → Run → Repeat. This is the cycle you'll follow thousands of times as a developer.

5. What happens when you run dotnet run for the first time after creating a new console project with .NET 10?

Show answer

Correct: C

Why C is correct: This is the standard .NET execution pipeline:

  1. C# source code → compiled to Intermediate Language (IL) by the C# compiler
  2. At runtime, the CLR's JIT (Just‑In‑Time) compiler translates IL to native machine code
  3. The native code executes on the CPU

This two‑step process is what gives .NET its cross‑platform capabilities and performance characteristics.

Why A is incorrect: C# is not an interpreted language. It's compiled to IL, then JIT‑compiled to native code. There's no line‑by‑line interpretation.

Why B is incorrect: C# does not compile directly to native machine code. It compiles to IL first, and the JIT compiler handles the native code translation at runtime. This is known as managed code.

Why D is incorrect: .NET compilation happens entirely on your local machine. The .NET SDK includes everything needed to compile and run your code — no internet or cloud compilation is required.

Reinforcement: The IL → JIT → Native pipeline is the heart of .NET. It's why your code runs on any platform with .NET installed, and why the JIT can optimise the code for your specific CPU at runtime.

You've written your first C# application and understand the entire development lifecycle!


dotnetmadeeasy.com — Learn C# and .NET, the right way.