Skip to content

C# - Environment

This chapter guides you through setting up a modern development environment for C# using the latest tools and best practices. C# is a powerful, cross-platform language that is part of the .NET ecosystem. Before writing code, let’s understand the core components you’ll need.

The modern .NET (previously known as .NET Core) is an open-source, cross-platform framework for building a wide variety of applications, including:

  • Web Applications and APIs: Using ASP.NET Core.
  • Desktop Applications: Using MAUI, WPF, or Windows Forms.
  • Cloud-Native Apps: Microservices, serverless functions, and containerized apps.
  • Mobile Applications: Using .NET MAUI.
  • Games: Using game engines like Unity or Godot.
  • IoT and AI/ML applications.

The key component you need to install is the .NET SDK (Software Development Kit). It includes everything required to build and run .NET applications:

  • The .NET CLI: A command-line interface for creating, building, testing, and publishing applications.
  • The .NET Runtimes: Necessary to run the compiled applications.
  • The Base Class Library (BCL): A massive library of reusable code for common tasks.

The first and most crucial step is to download and install the latest .NET SDK. You can always find the latest version on the official .NET download page: https://dotnet.microsoft.com/download.

  1. Download the recommended .NET SDK installer for Windows.
  2. Run the .exe installer and follow the on-screen instructions. The defaults are usually sufficient.
  3. Open a new Command Prompt or PowerShell terminal and verify the installation by running:
dotnet --version

You should see an output like 8.0.300 (the version number will vary).

  1. Download the appropriate installer for your Mac (x64 or Arm64).
  2. Run the .pkg file and follow the setup wizard.
  3. Alternatively, if you use Homebrew, you can install it via the terminal:
brew install dotnet-sdk
  1. Open a new Terminal and verify the installation:
dotnet --version
  1. Open a terminal and run the following commands to install the SDK (this example is for .NET 8 on Ubuntu 22.04; check the official Microsoft docs for other versions/distributions):
sudo apt-get update && \
sudo apt-get install -y dotnet-sdk-8.0
  1. Verify the installation:
dotnet --version

While you can write C# in any text editor, using an Integrated Development Environment (IDE) or a code editor with good C# support will significantly boost your productivity. Here are the top choices:

Editor/IDEBest ForPlatformCost
Visual Studio 2022Full-featured, powerful IDE for all types of .NET development.WindowsFree (Community Edition)
Visual Studio Code (VS Code)Lightweight, fast, and highly extensible code editor.Windows, macOS, LinuxFree
JetBrains RiderFast and powerful cross-platform .NET IDE.Windows, macOS, LinuxPaid (with free trials)

Recommendation for Beginners: Start with Visual Studio Code for a lightweight, cross-platform experience, or Visual Studio Community on Windows for a fully integrated, powerful setup.

If you choose VS Code, you must install the official C# extension pack for the best experience:

  1. Open VS Code.
  2. Navigate to the Extensions view (or press Ctrl+Shift+X).
  3. Search for “C# Dev Kit”.
  4. Install the extension pack published by Microsoft. It includes everything you need for C# development, debugging, and project management.

Step 3: Create and Run Your First C# Program

Section titled “Step 3: Create and Run Your First C# Program”

The .NET CLI makes it incredibly easy to create a new project from a template.

  1. Open your terminal or command prompt.
  2. Create a new directory for your project and navigate into it:
mkdir MyFirstApp
cd MyFirstApp
  1. Use the dotnet new command to create a new console application:
dotnet new console

This creates a minimal C# project, including a MyFirstApp.csproj project file and a Program.cs file.

  1. Open Program.cs in your chosen editor. It will contain a simple “Hello, World!” program:

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

1. To run your application, simply execute the following command in your terminal:

dotnet run

You will see the output:

Hello, World!

## Modern Development Workflow
As you build more complex applications, you'll rely on these essential tools:
### 1. NuGet Package Manager
**NuGet** is the package manager for .NET. It allows you to easily add third-party libraries (packages) to your project. For example, to add the popular `Newtonsoft.Json` library for working with JSON, you would run:

dotnet add package Newtonsoft.Json

### 2. Git for Version Control
Version control is crucial for managing your codebase. To start tracking your project with Git, navigate to your project's root directory and run:

git init git add . git commit -m “Initial commit: new console app”

It's also a good practice to add a `.gitignore` file to your project to exclude build artifacts. You can create one easily with the CLI: `dotnet new gitignore`.