<< Home

Chapter 1: Welcome to C# and .NET

C# sits neatly in the family of modern, strongly typed languages that aim to make software development clear, expressive, and safe. It began life within the wider .NET ecosystem, which provides a broad foundation of libraries, tools, and runtime features that help developers build everything from console utilities to large web services. This chapter prepares your environment, introduces the tools, and guides you through your first working program.

What C# is and where it fits

C# is a general purpose programming language created by Microsoft and shaped over several generations of improvements. It blends object oriented design, expressive functional constructs, and a memory model that reduces many common errors. It runs on the .NET platform, which is an open source and cross platform framework supported on Windows, macOS, and Linux.

The language is used for desktop apps, server side systems, cloud tools, games, mobile apps, command line utilities, and more. You can think of it as a tool with several edges. It handles high level patterns with clean syntax and also supports lower level control when needed.

💡 C# and .NET are fully open source at https://github.com/dotnet which means you can explore the runtime source code to understand exactly how platform features work.

How C# relates to C

C# looks as if it grew from the same branch as C since the surface features share familiar shapes. Braces group code, semicolons complete statements, and control structures such as if, while, and for follow a pattern that echoes the older language. These shared elements help newcomers feel at home when they first read C# code because the visual style is recognisable.

The deeper structure is different. C works directly with memory, pointers, and manual resource handling while C# lives inside the managed .NET runtime which handles allocation and cleanup automatically. This means many of the tricky parts of low level programming simply do not appear in C#. Features such as pattern matching, properties, async and await, delegates, generics, and the LINQ query model sit far outside the scope of traditional C. They emerge from a different design philosophy that focuses on safety, clarity, and expressiveness.

You can think of C# as a language that keeps the outer style of the C family while running on a far more structured and protective foundation. The result feels familiar at first glance although the way you write and reason about programs soon diverges from the C approach.

Installing the .NET SDK and the CLI

To write and run C# programs you need the .NET SDK. It contains the compiler, the dotnet command line interface, and the base class libraries. Installation is simple and consistent across supported platforms.

You can download installers or packages directly from the official site at dotnet.microsoft.com/download. After installation, open a terminal and check that everything is ready by running the following command.

dotnet --version

This prints the SDK version number. If you see a valid version string, your setup is complete. The CLI is your primary tool for creating projects, restoring dependencies, building code, and running applications.

⚠️ Some Linux distributions provide their own .NET builds. These can lag behind the official releases and occasionally introduce packaging differences. The official installers are usually the most reliable choice.

Your first console application

Every new language feels calmer once you have created a small working program. The dotnet CLI includes a template system that makes this process simple.

Start by creating a new folder for your project, then run the following commands.

mkdir HelloWorld
cd HelloWorld
dotnet new console

The template generates a small program file named Program.cs. Open it in your editor. You will see a structure similar to this.

Console.WriteLine("Hello world");

You can run your program immediately.

dotnet run

This prints the greeting text to the console. Although the project contains more files than this single line of code, the default layout keeps everything organised so you can grow the project later.

Project structure and the build process

Every .NET project generated by the CLI follows a predictable pattern. The root directory contains a project file with the extension .csproj. This file describes the project type, dependencies, target frameworks, and build settings. Your source files sit alongside it or in subfolders. During compilation the CLI gathers all source files, resolves dependencies, and produces a build output in the bin directory.

A typical layout looks like this.

HelloWorld/
  ├─ Program.cs
  ├─ HelloWorld.csproj
  ├─ bin/
  └─ obj/

The obj directory holds intermediate files used during the build while bin stores the compiled output. When your projects become larger this structure keeps the noise away from your source tree.

Source files, namespaces, and using directives

C# code is organised into namespaces. A namespace acts like a folder system for types which helps avoid naming conflicts and keeps related concepts close together. Within a file you can either declare a namespace explicitly or rely on modern file scoped syntax.

When you need types from other namespaces, you import them using using directives. This keeps code readable and reduces repetition. For example, using System allows you to refer to Console directly rather than typing the fully qualified name each time.

💡 You can group related files into folders without affecting their namespaces. Folder structure and namespace structure are separate concepts although many developers follow a matching pattern.