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.
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:
- Install VS Code
- Check .NET SDK version
- Download the .NET SDK Installer File
- Run the Installer
- 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
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.
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.
Once the installation is complete, you will see this screen:
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.
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:
- Install VS Code
- Check the .NET SDK version
- Download the .NET SDK Installer File
- Run the Installer
- Follow the Instructions
- 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.
Step 4: Run the Installer
Go to your downloads folder and run the installer you just downloaded.
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:
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
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:
- Install VS Code
- Install SDK and Run Time
- 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
and then run,
sudo dpkg -i packages-microsoft-prod.deb
Again, download the required dependencies,
sudo apt-get install -y apt-transport-https
Now, you can install the .NET SDK by running the following command.
sudo apt-get install -y dotnet-sdk-8.0
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
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.
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
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.
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
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.