Getting Started with C#

C# is a versatile programming language for various applications including complex backends, desktop software, web applications, and interactive games.

You can run C# on your computer using the following two methods:

  • Run C# online
  • Install C# on your computer

In this tutorial, you will learn both methods.

Run C# Online

To run C# code, you must have the .NET SDK installed on your system. However, you can use our free online C# compiler if you want to start immediately.

Online C# Compiler
Online C# Compiler

The online editor enables you to run C# code directly in your browser—no installation required.


Install C# on Your Computer

For those who prefer to install C# on their computer, this guide will walk you through the installation process on Windows, macOS, or Linux (Ubuntu).

To install C# on your Windows, just follow these steps:

  1. Install VS Code
  2. Check .NET SDK version
  3. Download the .NET SDK Installer File
  4. Run the Installer
  5. Verify the installation

Here is a detailed explanation of each of the steps:

Step 1: Install VS Code

Go to the VS Code official website and download the Windows installer. Once the download is complete, run the installer and follow the installation process.

Click Finish to complete the installation process.

Step 2: Check .NET SDK Version

Before running C# code, you need to ensure the .NET SDK is installed on your computer as it provides the necessary tools and environment.

Open the Command Prompt and run the following command to check if the .NET SDK is already installed

dotnet --version
dotnet Version
dotnet Version

If the command returns a version, you have the SDK installed; if not, proceed to download and install the .NET SDK.

Step 3: Download the .NET SDK Installer

Visit the .NET download page on your web browser. The website automatically detects your operating system and gives you the right installer.

Simply click on the recommended download link to get the correct installer.

.NET Download Page
.NET Download Page

Step 4: Run the .NET SDK Installer

After the download is complete, look for the file you just downloaded. Double-click the file to open the installer.

Now, click Install to start the installation.

.NET SDK Installer
.NET SDK Installer

Once the installation is complete, you will see this screen:

dotnet Installation Successful
dotnet Installation Successful

Click Close to exit the installer.

Step 5: Verify the Installation

After installation, open a new Command Prompt window to make sure everything's set up correctly.

Run the following command once more

dotnet --version

and it should now display the version of the SDK you've installed.

dotnet Version
dotnet Version

Note: The version number might differ from the one above, depending on your installed version.

Now, you are all set to run C# programs on your device.

To install C# on your Mac, just follow these steps:

  1. Install VS Code
  2. Check the .NET SDK version
  3. Download the .NET SDK Installer File
  4. Run the Installer
  5. Follow the Instructions
  6. Verify the installation

Here is a detailed explanation of each of the steps:

Step 1: Install VS Code

Go to the VS Code official website and download the zipped file. Once the download is complete, open the zipped file.

In Finder, open a new window and navigate to the Applications folder. Drag the VS Code application from the zip file into the Applications folder to install it.

You can now launch VS Code directly from the Applications folder.

Step 2: Check the .NET SDK Version

To build and run C# applications, you need the .NET SDK. You can check if .NET SDK is already installed by using the following command in the Terminal app:

dotnet --version

If you are satisfied with the installed version, you can skip the remaining steps. Otherwise, follow the steps below.

Step 3: Download the .NET SDK Installer File

Visit the official .NET website and download the latest version of the .NET SDK for macOS.

The website automatically detects your operating system and gives you the right installer.

C# Download Page for Mac
C# Download Page for Mac

Step 4: Run the Installer

Go to your downloads folder and run the installer you just downloaded.

C# Run Installer for Mac
C# Run Installer for Mac

Step 5: Follow the Instructions

You will be prompted to agree to the software license agreement, choose the installation location (we recommend using the default location), and enter your administrator password.

Simply proceed through the installation process.

Once the installation is complete, you will see this screen:

dotnet Installation Successful
dotnet Installation Successful

Click Close to exit the installer.

Step 6: Verify the Installation

Once the installation is complete, you can verify whether .NET SDK is installed by using the following command in the Terminal app:

dotnet --version
C# Installation Verification for Mac
C# Installation Verification for Mac

Note: The version number might differ from the one above, depending on your installed version.

Linux has various distributions, and the installation process differs slightly from each other. For now, we will focus on Ubuntu.

To install C#, follow these steps:

  1. Install VS Code
  2. Install SDK and Run Time
  3. Verify the Installation

Here is a detailed explanation of each of the steps:

Step 1: Install VS Code

Open the Terminal and type

sudo apt update

This command updates your package lists to ensure you receive the latest versions of your software.

Proceed to install VS Code with

sudo snap install code --classic

Step 2: Install .NET SDK

Now, we'll install the .NET SDK, which includes everything you need to build and run C# applications.

But first, download and install the Microsoft package signing key:

wget https://packages.microsoft.com/config/ubuntu/21.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
Install MS Package Signing Key
Install MS Package Signing Key

and then run,

sudo dpkg -i packages-microsoft-prod.deb
Install MS packages on Ubuntu
Install MS packages on Ubuntu

Again, download the required dependencies,

sudo apt-get install -y apt-transport-https
Install apt-transport-https package in ubuntu
Install apt-transport-https package in ubuntu

Now, you can install the .NET SDK by running the following command.

sudo apt-get install -y dotnet-sdk-8.0
Install .NET SDK in ubuntu
Install .NET SDK in ubuntu

Note: Replace 8.0 with the version you need if you're installing a different version of the .NET SDK.

Step 3: Verify the Installation

To ensure that the .NET SDK has been installed correctly, verify the installation by checking the version of the SDK using the following command,

dotnet -–version
Dotnet Installation Verification for Linux
Dotnet Installation Verification for Linux

Now, you are all set to run C# programs on your device.


Run Your First C# Program

There are a few things you'll need to set up for running your first C# program.

  • Install the C# Extension in VS Code
  • Create a .NET Console Project
  • Write Your C# Program
  • Run Your Program

Install the C# Extension in VS Code

Before you begin coding, ensure the C# extension is installed in VS Code. Open VS Code and click on Extensions on the left sidebar. Search for the C# extension by Microsoft and click on Install.

C# Extension by Microsoft
C# Extension by Microsoft

This extension provides C# language support, debugging, and more, helping you develop .NET applications efficiently.

Create a .NET Console Project

Before you write any C# code, you need a structured environment where that code can live and be executed. Creating a .NET Console Project sets up this environment.

For that, run the following command in the VS Code's terminal,

dotnet new console -n HelloWorld
Create dotnet Console Project
Create dotnet Console Project

This command sets up a new console project named HelloWorld for you. Once that's done, open the HelloWorld folder in VS Code to see your new project files.

HelloWorld Console Project
HelloWorld Console Project

Note: You can open the VS Code terminal by pressing the keyboard shortcut Ctrl + `.

Write Your C# Program

Now that you have your project set up, it's time to start coding. Open the Program.cs file located in your project directory and write the following code:

using System;

class Program {
    static void Main() {
        Console.WriteLine("Hello World");
    }
}

Run Your Program

Now it's time to run your first C# program. Head back to the terminal and run

dotnet run 
Run Your Program
Run Your Program

You should see Hello World output to the console.

Now that you have set everything up to run the C# program on your computer, you'll be learning how the basic program works in C# in the next tutorial.

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges