Setup and your first program
Install the .NET 10 SDK, pick an editor and run your first program. Along the way you learn why dotnet run needs a project, what Program.cs and the .csproj file contain, and what dotnet run really does.
2.1 Install the .NET 10 SDK
- The SDK is the developer toolkit: compiler + dotnet CLI + runtime together.
- The runtime alone can only run apps; to build code you need the SDK.
- After installing, open a new terminal: dotnet --version should start with 10.0.
Terminal
# Windowswinget install Microsoft.DotNet.SDK.10 # macOSbrew install --cask dotnet-sdk # checkdotnet --versionOutput
$ dotnet --version10.0.4032.2 Choose an editor
| Editor | Cost | Platforms |
|---|---|---|
| VS Code + C# Dev Kit | Free | Windows, macOS, Linux |
| Visual Studio 2026 Community | Free for students | Windows (full IDE) |
| JetBrains Rider | Free for non-commercial use | Windows, macOS, Linux |
- All three use the same dotnet SDK underneath, so the commands in this course work everywhere.
2.3 Problem: dotnet run fails in an empty folder
Terminal
mkdir hellocd hellodotnet runOutput
$ dotnet runCouldn't find a project to run.Ensure a project exists in ...\hello, or passthe path to the project using --project.- dotnet run needs to know what to build and run; that comes from the project file (.csproj).
- An empty folder has no .csproj and no code file, so the CLI finds nothing.
- Fix: create a project first with dotnet new.
2.4 Fix: dotnet new console
- dotnet new console -n HelloApp creates a HelloApp folder from the Console App template.
- It contains Program.cs and HelloApp.csproj, and a restore runs automatically.
Terminal
dotnet new console -n HelloAppcd HelloAppdotnet runOutput
$ dotnet new console -n HelloAppThe template "Console App" was created successfully. Processing post-creation actions...Restoring ...\HelloApp\HelloApp.csproj:Restore succeeded. $ cd HelloApp$ dotnet runHello, World!2.5 First program: Program.cs
- Console.WriteLine prints text and moves to the next line.
- Console.Write stays on the same line, so the user types right after the prompt.
- Console.ReadLine returns the typed text (here stored in name).
- $"...{name}" is string interpolation: it puts the value inside the message.
C#
Console.WriteLine("Hello, World!"); Console.Write("Aapka naam kya hai? ");var name = Console.ReadLine(); Console.WriteLine($"Welcome to C#, {name}!");Output
$ dotnet runHello, World!Aapka naam kya hai? RahulWelcome to C#, Rahul!2.6 Behind top-level statements
- Program.cs has no class and no Main method: this is called top-level statements.
- The compiler generates a Program class and a Main method for you behind the scenes.
- Older tutorials use the classic shape with static void Main as the entry point.
- dotnet new console --use-program-main still gives you the classic template; both print the same output.
C#
namespace ClassicApp; class Program{ static void Main(string[] args) { Console.WriteLine("Hello, World!"); }}2.7 .csproj: the project's recipe card
XML
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net10.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> </Project>| Setting | Meaning |
|---|---|
| OutputType Exe | A runnable program (not a library) |
| TargetFramework net10.0 | Runs on .NET 10 |
| ImplicitUsings enable | Common namespaces like System are imported for you (no using System for Console) |
| Nullable enable | The compiler warns about possible null problems |
2.8 Inside dotnet run
- Step 1
1.dotnet run: looks for the .csproj in the folder
- Step 2
2.Restore: downloads NuGet packages, details kept in obj/
- Step 3
3.Build: compiles C# to IL -> bin/Debug/net10.0/HelloApp.dll
- Step 4
4.Run: the CLR loads the .dll and the JIT makes machine code
- Step 5
5.Program output appears in the terminal
dotnet run = restore + build + run
- Checkpoint: dotnet build alone does not print Hello, World; it only restores and compiles.
- To run a built program, pass the .dll path to dotnet.
Output
$ dotnet buildRestore complete (1.5s) HelloApp net10.0 succeeded (2.1s) → bin\Debug\net10.0\HelloApp.dll Build succeeded in 5.5s $ dotnet bin/Debug/net10.0/HelloApp.dllHello, World!Aapka naam kya hai? PriyaWelcome to C#, Priya!2.9 Common mistake: top-level statements in two files
- Writing Console.WriteLine directly in a second file (Greeting.cs) breaks the build with CS8802.
- A program has exactly one entry point, so only one file may have top-level statements.
- Fix: put the extra code in a class and call it from Program.cs.
C#
// Program.csConsole.WriteLine("Hello, World!");Greeting.Say("Rahul"); // Greeting.cspublic static class Greeting{ public static void Say(string name) => Console.WriteLine($"Hello, {name}!");}2.10 .NET 10: file-based apps
- For a quick script, no project is needed: dotnet run orders.cs runs a single file.
- #:package adds a NuGet package (here Humanizer) right inside the file.
- When the script grows, dotnet project convert turns it into a normal project.
C#
#:package Humanizer@2.14.1using Humanizer; var lastOrder = DateTime.UtcNow.AddHours(-5);var ago = lastOrder.Humanize(); Console.WriteLine("Quick script, no .csproj");Console.WriteLine($"Last order: {ago}");Output
$ dotnet run orders.csQuick script, no .csprojLast order: 5 hours ago $ dotnet project convert orders.cs$ ls ordersorders.cs orders.csproj2.11 Setup rules and misconceptions
- Install the LTS SDK (.NET 10) and check it with dotnet --version.
- One folder per project, created with dotnet new.
- dotnet run = restore + build + run; dotnet build only compiles.
- Windows and Visual Studio are not required: VS Code + terminal works the same on Mac and Linux.
- Top-level statements are not just for beginners: real ASP.NET Core Program.cs files use them too.
2.12 Interview tips
- Top-level statements: code without a class + Main; the compiler generates the Program class and Main.
- Only one file can have top-level statements because a program has exactly one entry point (else CS8802).
- dotnet run does restore, build and run; dotnet build only compiles to bin/.
- SDK vs runtime: the SDK builds and runs, the runtime only runs.