Setting Up the Development Environment
Setting up a development environment is like preparing an art studio — once the canvas (.NET SDK) and brushes (VS Code) are in place, you can start creating (writing C# code).
Installing the .NET SDK
The .NET SDK (Software Development Kit) is the core toolkit for developing C# programs, containing the compiler, runtime, and project management tools. You need to install it first before you can write, compile, and run C# programs.
Installing on Windows
- Visit https://dotnet.microsoft.com/download/dotnet/8.0 to download the .NET 8 SDK installer
- Run the installer and complete the installation with the default options
- Open a command prompt and verify:
dotnet --version
8.0.xxx
If you see a version number, the installation was successful.
Installing on macOS
Install via Homebrew:
brew install dotnet-sdk
Or download the macOS installer from the official website. Verify the same way:
dotnet --version
Installing on Linux
Ubuntu/Debian:
sudo apt-get update
sudo apt-get install -y dotnet-sdk-8.0
CentOS/RHEL:
sudo dnf install -y dotnet-sdk-8.0
Verify the installation:
dotnet --version
💡 Tip: This book uses .NET 8 (LTS — Long-Term Support). Make sure to install the SDK, not just the Runtime. The SDK includes all the tools needed for development.
Key Components Included in the SDK
| Component | Role |
|---|---|
| Roslyn Compiler | Compiles C# code into Intermediate Language (IL) |
| dotnet CLI | Command-line tool for creating, building, and running projects |
| .NET Runtime | Executes compiled programs |
| NuGet Package Manager | Manages third-party library dependencies |
Configuring VS Code
VS Code is a free code editor developed by Microsoft. Paired with the C# extension, it provides a powerful C# development experience.
Installing VS Code
Go to https://code.visualstudio.com to download and install.
Installing Required Extensions
Open VS Code, press Ctrl+Shift+X to open the Extensions marketplace, then search for and install:
| Extension | Purpose |
|---|---|
| C# Dev Kit (Microsoft) | All-in-one support for syntax highlighting, IntelliSense, debugging, and project management |
| C# (Microsoft) | Basic C# language service (usually installed automatically with Dev Kit) |
Configuring C# Dev Kit
After installing C# Dev Kit, the extension automatically detects your installed .NET SDK. You can confirm the configuration is correct by:
- Press
Ctrl+Shift+P, type.NET: New Project— if a project template list appears, the setup is successful - The bottom status bar should display the .NET SDK version number
⚠️ Warning: When opening a folder in VS Code, create a dedicated folder (e.g., csharp-learn) and open the entire folder in VS Code rather than a single file. This is required for C# Dev Kit to work properly.
Your First Program: Hello World
Example
Use the dotnet CLI to create your first console application:
dotnet new console -n MyFirstApp
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring packages...
Determining projects to restore...
Restored MyFirstApp.csproj (in 1.2s).
Navigate into the project directory and run it:
cd MyFirstApp
dotnet run
Hello, World!
That's your first C# program! The dotnet new console command automatically generates the project structure and code.
Project Structure
After creation, the project folder contains:
MyFirstApp/
├── MyFirstApp.csproj
├── Program.cs
└── obj/
└── ...
| File | Purpose |
|---|---|
Program.cs |
Program entry point, contains the main code |
MyFirstApp.csproj |
Project configuration file, defines SDK, target framework, etc. |
obj/ |
Intermediate build output directory |
Common dotnet CLI Commands
The dotnet CLI is the core command-line tool for .NET development. Mastering it is more efficient than relying on an IDE.
Project Management Commands
| Command | Purpose | Example |
|---|---|---|
dotnet new console |
Create a console project | dotnet new console -n MyApp |
dotnet new classlib |
Create a class library project | dotnet new classlib -n MyLib |
dotnet new list |
List all available templates | dotnet new list |
The -n parameter specifies the project name. If omitted, the current folder name is used.
Build, Run, and Publish
| Command | Purpose | Example |
|---|---|---|
dotnet build |
Compile the project (without running) | dotnet build |
dotnet run |
Compile and run the project | dotnet run |
dotnet publish |
Publish the project (for deployment) | dotnet publish -c Release |
Example
dotnet build
MSBuild version 17.8.xxxx
Determining projects to restore...
Restored MyFirstApp.csproj (in 102 ms).
MyFirstApp -> G:\csharp-learn\MyFirstApp\bin\Debug\net8.0\MyFirstApp.dll
Build succeeded.
0 Warning(s)
0 Error(s)
🔥 Tip: dotnet run = dotnet build + automatic execution. Use dotnet run for quick testing during development, and dotnet build to check for compilation issues in formal builds.
Top-Level Statements vs. Traditional Program.Main
C# 9 introduced top-level statements, dramatically simplifying console program syntax. Understanding both styles helps you read C# code from different eras.
Top-Level Statements (C# 9+, the Default)
dotnet new console generates a Program.cs that uses top-level statements by default:
System.Console.WriteLine("Hello, World!");
Just one line — no namespace, no class, no Main method. The compiler automatically wraps it in a generated Main method.
Traditional Program.Main Style
The pre-C# 9 approach, where all code must go inside a Main method:
using System;
namespace MyFirstApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
Comparing the Two Styles
| Feature | Top-Level Statements | Traditional Program.Main |
|---|---|---|
| Code volume | Very little | More boilerplate |
| Entry method | Auto-generated by compiler | Manually defined static void Main |
using declarations |
Can be omitted (use fully qualified names) | Usually need using System; |
| Namespace | No declaration needed | Must declare |
| Command-line arguments | Access via args variable directly |
Via Main(string[] args) parameter |
| Use cases | Simple programs, learning, scripting | Large projects needing fine-grained entry control |
| C# version | C# 9+ | All versions |
📌 Key Point: Top-level statements and the traditional style are functionally equivalent. Top-level statements are just syntactic sugar — the generated IL code is essentially the same. Only one file in a project can use top-level statements.
Accessing Command-Line Arguments with Top-Level Statements
if (args.Length > 0)
{
System.Console.WriteLine($"Hello, {args[0]}!");
}
else
{
System.Console.WriteLine("Hello, World!");
}
dotnet run -- Alice
Hello, Alice!
Arguments after -- are passed to the program. In top-level statements, args is automatically injected by the compiler.
Common Compiler Errors
As a beginner, you will definitely encounter compiler errors. Don't panic — learning to read error messages is more important than avoiding them.
CS1002: Semicolon Expected
System.Console.WriteLine("Hello, World!")
Program.cs(1,40): error CS1002: ; expected
The error format is filename(line,column): error code: description. This line is missing a semicolon at the end — every C# statement must end with a semicolon.
CS0103: Name Does Not Exist in the Current Context
Console.WriteLine("Hello, World!");
Program.cs(1,1): error CS0103: The name 'Console' does not exist in the current context
You used Console without using System; or the fully qualified name System.Console. Two solutions: add using System; or use System.Console.
CS0117: Does Not Contain a Definition
System.Console.WritLine("Hello, World!");
Program.cs(1,17): error CS0117: 'Console' does not contain a definition for 'WritLine'
WriteLine was misspelled as WritLine. The compiler is telling you that the Console class has no member called WritLine — check your spelling.
CS0120: Object Reference Required for Non-Static Field
namespace MyApp
{
class Program
{
string name = "C#";
static void Main(string[] args)
{
System.Console.WriteLine(name);
}
}
}
Program.cs(8,37): error CS0120: An object reference is required for the non-static field, method, or property 'Program.name'
You accessed a non-static field name from the static method Main. Static methods belong to the class itself, while non-static members belong to instances of the class — you need to create an object first.
⚠️ Tip for reading error messages: The reported line number may point to the position after the actual mistake. If a line shows an error, check that line and the one above it for spelling, semicolons, and brackets. CS codes are official Microsoft error codes — you can search for a CS number online to find detailed explanations.
❓ FAQ
dotnet run and dotnet build?dotnet build only compiles the project and produces DLL files without executing. dotnet run compiles (if needed) and then automatically runs the program. Use dotnet run for convenient development debugging, and dotnet build for more controlled CI/CD builds.📖 Summary
- .NET 8 SDK is the core toolkit for C# development, installable on Windows/macOS/Linux, verified with
dotnet --version - VS Code with the C# Dev Kit extension provides a lightweight yet powerful C# development environment
- The dotnet CLI is the core project management tool:
dotnet newto create,dotnet buildto compile,dotnet runto execute - Top-level statements (C# 9+) are a simplified style that is functionally equivalent to the traditional Program.Main — beginners should start with top-level statements
- Compiler error messages contain the filename, line number, CS code, and description — learning to read them is a core skill
📝 Exercises
- Install the .NET 8 SDK on your computer and verify that
dotnet --versionoutputs correctly. Record the version number - Use
dotnet new console -n Homework02to create a project. Modify Program.cs to output your name and a greeting (e.g., "Hello, I'm Alex, starting to learn C#!"), then run it and take a screenshot - Rewrite the top-level statements version from exercise 2 in the traditional Program.Main style, and confirm both produce the same output
- Intentionally introduce 3 compiler errors (missing semicolon, misspelling, omitted using), record the CS codes and error descriptions, and practice reading error messages



