Getting Started with C#
Welcome to the C# documentation. This guide will help you set up your development environment and write your first C# program.
1. Prerequisites
Before you begin, ensure you have the following:
- A computer running Windows, macOS, or Linux.
- An internet connection to download the .NET SDK.
2. Installing the .NET SDK
C# is a .NET language, so you'll need the .NET SDK (Software Development Kit). The SDK includes the C# compiler, the .NET runtime, and various tools.
- Visit the official .NET download page.
- Download the latest stable LTS (Long-Term Support) or Standard Term Support (STS) version of the .NET SDK for your operating system.
- Run the installer and follow the on-screen instructions.
3. Verifying the Installation
Open your terminal or command prompt and run the following command to verify that the .NET SDK is installed correctly:
dotnet --version
You should see the installed .NET SDK version number printed in the console.
4. Your First C# Program: "Hello, World!"
Let's create a simple C# application. We'll use the .NET CLI (Command Line Interface) for this.
Step 1: Create a New Project
Navigate to the directory where you want to create your project in the terminal and run:
dotnet new console -o HelloWorldApp
This command creates a new directory named HelloWorldApp
and generates a basic console application template inside it.
Step 2: Navigate to the Project Directory
cd HelloWorldApp
Step 3: Examine the Code
Open the Program.cs
file in your favorite text editor or IDE. It should look like this:
using System;
// The namespace declaration is a container for your code.
namespace HelloWorldApp
{
class Program
{
static void Main(string[] args)
{
// This line prints "Hello, World!" to the console.
Console.WriteLine("Hello, World!");
}
}
}
Step 4: Run the Application
From within the HelloWorldApp
directory in your terminal, run:
dotnet run
You should see the following output:
Hello, World!
Congratulations!
You've successfully installed the .NET SDK and written and executed your first C# program. You're now ready to explore more advanced C# concepts.