Getting Started with .NET
Welcome to C#! C# (pronounced "C Sharp") is a modern, object-oriented, and type-safe programming language developed by Microsoft. It's the primary language for building applications on the .NET platform.
What is .NET?
.NET is a free, open-source developer platform for building many different types of applications. It consists of:
- The Runtime (CLR): Handles memory management (Garbage Collection), security, and compilation.
- The Libraries: A massive set of standard classes for everything from file I/O to web servers.
- The SDK: The tools needed to develop applications.
Installing the .NET SDK
Before you can write any C# code, you must install the .NET SDK (Software Development Kit). The SDK includes everything you need to build and run .NET applications.
- Download: Visit the official .NET download page.
- Select Version: It is generally recommended to use the latest LTS (Long Term Support) version for stability.
- Verify Installation: Open your terminal (Command Prompt, PowerShell, or zsh) and type:
If successful, you will see a version number (e.g.,dotnet --version8.0.100).
Choosing Your Development Environment
You have two main paths for C# development. Both are free, but they serve different needs.
Option 1: Visual Studio Code (Lightweight & Cross-Platform)
Ideal for: Beginners, web developers, and those on macOS or Linux.
1. Install VS Code
Download it from code.visualstudio.com.
2. Install the C# Dev Kit
To get the best experience, go to the Extensions view (Ctrl+Shift+X) and install:
- C# Dev Kit: The official extension from Microsoft for IntelliSense, debugging, and project management.
- IntelliCode for C#: Provides AI-assisted code completions.
3. Setting Up a Project
You don't just "create a file" in .NET; you create a Project. Open your terminal in VS Code and run:
dotnet new console -n MyFirstApp
cd MyFirstApp
code . -r
Option 2: Visual Studio Community (Full-Featured IDE)
Ideal for: Windows users, complex enterprise applications, and those who want a "all-in-one" GUI-driven experience.
1. Download & Install
Download the Community Edition (which is free for individual developers and small teams) from visualstudio.microsoft.com.
2. Using the Visual Studio Installer
Unlike VS Code, Visual Studio uses an "Installer" where you select Workloads. To follow this course, you should check:
- .NET desktop development: Includes everything for console apps and Windows desktop apps.
- ASP.NET and web development: (Optional) For building websites and APIs.
3. Key Features
- Powerful Debugger: Visual Studio has the world-class "Diagnostic Tools" and "Live Visual Tree" for deep debugging.
- Integrated Project Management: You can manage NuGet packages, database connections, and Git branches all from within the GUI (Solution Explorer).
- Visual Designers: Built-in drag-and-drop editors for UI frameworks like WinForms and WPF.
The Evolution of the "Main" Method
The Traditional Way (C# 1.0 - 8.0)
Historically, every C# program required a "Boilerplate" structure. Even to print one line of text, you needed a Namespace, a Class, and a Static Method:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, .NET!");
}
}
}
Top-Level Statements (C# 9.0 and Later)
Starting with C# 9, Microsoft introduced Top-Level Statements to reduce "ceremony" and make the language more approachable for beginners.
Now, your Program.cs can simply be:
Console.WriteLine("Hello, .NET!");
How it works "Under the Hood"
The compiler is doing the work for you. When you use top-level statements, the C# compiler automatically wraps your code inside a static void Main method within a hidden class.
Key Rules for Top-Level Statements:
- Only One File: Only one file in your entire project can use top-level statements. This file becomes the "entry point" of your application.
- Accessing Arguments: Even though you don't see
string[] args, a magic variable namedargsis automatically available for reading command-line inputs.if (args.length > 0) { Console.WriteLine($"Hello, {args[0]}!"); } - Async Support: You can use
awaitdirectly at the top level without wrapping it in anasyncmethod. - Implicit Namespaces: In modern .NET templates, many common namespaces like
Systemare included automatically via "Global Usings," so you don't even needusing System;at the top.
Running Your Application
To run your code, use the .NET CLI (Command Line Interface) from your project folder:
dotnet build: Compiles the code and checks for errors.dotnet run: Compiles (if needed) and executes the application.dotnet watch: A "hot reload" tool that restarts your app automatically every time you save a file.