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:

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.

  1. Visit the official .NET download page.
  2. Download the latest stable LTS (Long-Term Support) or Standard Term Support (STS) version of the .NET SDK for your operating system.
  3. Run the installer and follow the on-screen instructions.
Note: For the best experience, it's recommended to install the latest version of the .NET SDK.

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!");
        }
    }
}
        
Tip: If you're using Visual Studio Code, you can install the C# extension for enhanced IntelliSense, debugging, and code navigation.

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.