Download and Install .NET SDK
To start programming in C#, you need to install the .NET Software Development Kit (SDK): 1. Visit the official Microsoft website: https://dotnet.microsoft.com/download 2. Download the latest .NET SDK for your operating system (Windows, macOS, or Linux) 3. Run the installer and follow the installation instructions 4. Verify the installation by opening a command prompt or terminal
Verify Installation
Open your command prompt or terminal and type: `dotnet --version` This should display the installed .NET version.
// Check .NET version
dotnet --version
6.0.100
Your First C# Program
Let's create a simple 'Hello World' program: 1. Create a new console project using `dotnet new console -n HelloWorld` 2. Navigate to the project directory 3. Run the program using `dotnet run`
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
Hello, World!
Understanding the Structure
• `using System;`: Imports the System namespace • `namespace HelloWorld`: Declares a namespace for organizing code • `class Program`: Defines a class (the filename doesn't need to match) • `static void Main(string[] args)`: The main method - entry point of C# programs • `Console.WriteLine()`: Prints output to the console • Curly braces `{}`: Define code blocks • Semicolon `;`: Ends statements
Choosing an IDE
While you can use a text editor and command line, IDEs make development easier: - Visual Studio: Full-featured IDE for Windows - Visual Studio Code: Lightweight cross-platform editor with C# extensions - Rider: Cross-platform .NET IDE from JetBrains - Visual Studio for Mac: IDE for macOS development
Expected Output
Hello, World!