Your First C# Program: Create, Build and Run It Step by Step
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.
- How do I set up my environment?
- Where do I write the code?
- How do I run it?
- What happens when I run it?
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.
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:
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:
- The C# compiler (
csc) - The .NET runtime (CLR)
- The Base Class Library (BCL)
- The
dotnetcommand-line tool
Download it from dotnet.microsoft.com/download and install it. After installation, open a terminal and verify:
10.0.100
Step 2 — Create a New Project
In your terminal, navigate to where you want to store your projects. Then run:
The template "Console App" was created successfully.
This command creates a new console application named MyFirstApp. Let's look at what it created:
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:
Console— A built-in .NET class that represents the terminal windowWriteLine— A method that prints text and moves to a new line"Hello, World!"— The text to display
This single line tells .NET: "Write this text to the console."
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:
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:
Console.WriteLine("Hello, World!");
- The C# compiler (
csc) readsProgram.cs - It translates your code to Intermediate Language (IL)
- The IL is stored in
bin/Debug/net10.0/MyFirstApp.dll
IL_0000: ldstr "Hello, World!"
IL_0005: call void [System.Console]System.Console::WriteLine(string)
IL_000A: ret
- The
dotnethost process starts - It loads the .NET runtime (CLR) into memory
- The CLR loads your
.dllassembly
- The first time
Console.WriteLineis called, the JIT compiler kicks in - It translates the IL for that method into native machine code
- The native code is cached in memory for future calls
- The native code runs on your CPU
Console.WriteLinecalls the operating system to display text- The text appears in your terminal
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>
- OutputType —
Exemeans this is an executable application - TargetFramework —
net10.0means we target .NET 10 - ImplicitUsings — automatically includes common namespaces (like
System) - Nullable — enables modern null‑safety features
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:
Hello, C# learner!
Welcome to .NET 10.
You've just made your first change! This is how all C# development works:
- Write code
- Run it with
dotnet run - See the result
- Iterate
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.
- The switch — Your
Program.csfile - Flipping the switch — Running
dotnet run - The electricity — .NET runtime executing your code
- The light — The output you see
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
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
$ cd ~/projects
$ dotnet run
Couldn't find a project to run.
$ 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:
- Start a new project — Every new app begins the same way
- Test a new idea — Create a console app to experiment with code
- Learn a new concept — Use a console app to practice without complexity
- Build a CLI tool — Many developer tools are console applications
When you might use something different:
- Web application — Use
dotnet new webfor ASP.NET Core - Class library — Use
dotnet new classlibfor reusable components - Mobile app — Use MAUI templates for cross‑platform mobile
The console app is your foundation — you'll always come back to it.
Mental Model
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
- Your first C# application is a console app — the simplest possible program.
- Use
dotnet new console -n <name>to create a new project. - Write code in
Program.cs— the entry point of your application. - Use
dotnet runto build and run your application in one command. - The compilation pipeline is: C# code → IL → JIT‑compiled native code → execution.
- Top‑level statements (C# 9+) simplify the code — no extra boilerplate.
- Every application starts here — this pattern scales to the most complex systems.
- The three essential commands:
dotnet new console— create a projectdotnet build— compile the codedotnet run— compile and run the code
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:
- Create the project with
dotnet new console - Write/Edit the code in
Program.cs - 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:
- C# source code → compiled to Intermediate Language (IL) by the C# compiler
- At runtime, the CLR's JIT (Just‑In‑Time) compiler translates IL to native machine code
- 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!