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.
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.
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.
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.
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.
Here's the journey your code takes — from your keyboard to a running application:
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.
First, you need the .NET SDK (Software Development Kit). This includes:
csc)dotnet command-line toolDownload it from dotnet.microsoft.com/download and install it. After installation, open a terminal and verify:
In your terminal, navigate to where you want to store your projects. Then run:
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.
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 displayThis 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.
In the terminal, make sure you're inside the MyFirstApp/ folder. Then run:
Congratulations! You've just written and run your first C# application.
Let's trace exactly what happens when you run dotnet run. This is the execution flow:
Console.WriteLine("Hello, World!");
csc) reads Program.csbin/Debug/net10.0/MyFirstApp.dll
IL_0000: ldstr "Hello, World!"
IL_0005: call void [System.Console]System.Console::WriteLine(string)
IL_000A: ret
dotnet host process starts.dll assemblyConsole.WriteLine is called, the JIT compiler kicks inConsole.WriteLine calls the operating system to display textHello, World!
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>
Exe means this is an executable applicationnet10.0 means we target .NET 10System)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:
You've just made your first change! This is how all C# development works:
dotnet runYou 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:
Console.WriteLine("Hello!");
→ Prints one line
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.
Your first C# application is like learning to turn on a light.
Program.cs filedotnet runIt's simple, but without this basic action, nothing else can work. Every complex system starts with a single action.
dotnet run does both — it builds and runs. It's your go‑to for development.
Wrong: Running dotnet run from the wrong folder
Correct: Navigate to the folder containing .csproj first
Wrong: Edit code, don't save, run → old output
Correct: Save (Ctrl+S) before running
WriteLine with WriteConsole.Write("Hello");
Console.Write("World");
→ Output: HelloWorld (no new line)
Console.WriteLine("Hello");
Console.WriteLine("World");
→ Output: Hello (new line) World
dotnet new web for ASP.NET Coredotnet new classlib for reusable componentsThe console app is your foundation — you'll always come back to it.
dotnet new console -n <name> to create a new project.Program.cs — the entry point of your application.dotnet run to build and run your application in one command.dotnet new console — create a projectdotnet build — compile the codedotnet run — compile and run the codeYou'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?
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");
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?
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:
Correct: A
Why A is correct: The correct workflow is:
dotnet new consoleProgram.csdotnet runThis 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?
Correct: C
Why C is correct: This is the standard .NET execution pipeline:
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.