C# Get Started
Getting started with C# involves setting up your development environment by installing the .NET SDK and choosing an Integrated Development Environment (IDE) like Visual Studio Code or Visual Studio Community/Professional. This guide provides step-by-step instructions for both Windows and Ubuntu users.
Key Topics
1. Introduction
C# is a modern, object-oriented programming language developed by Microsoft. To develop C# applications, you need the .NET SDK and an IDE. The .NET SDK includes the runtime and libraries needed to build and run .NET applications.
2. Installing .NET SDK
For Windows Users:
- Visit the official .NET website: https://dotnet.microsoft.com/download.
- Download the latest .NET SDK for Windows.
- Run the installer and follow the on-screen instructions.
For Ubuntu Users:
- Open the terminal.
- Add the Microsoft package signing key and repository:
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
- Install the .NET SDK:
sudo apt-get update
sudo apt-get install -y dotnet-sdk-6.0
3. Setting Up Environment Variables
Environment variables may need to be set to ensure the system recognizes the .NET commands.
For Windows Users:
- Press
Win + X
and select System. - Click on Advanced system settings.
- Click on Environment Variables.
- Under System Variables, find and select Path, then click Edit.
- Add the path to the .NET SDK (e.g.,
C:\Program Files\dotnet\
).
For Ubuntu Users:
The .NET installer should set up the environment variables automatically. If needed, you can add /usr/share/dotnet
to your PATH
variable in ~/.bashrc
or ~/.bash_profile
.
4. Installing Visual Studio Code and Extensions
For Windows and Ubuntu Users:
- Download Visual Studio Code from https://code.visualstudio.com/.
- Install Visual Studio Code by following the installer instructions.
- Open Visual Studio Code.
- Install the C# extension:
- Click on the Extensions icon on the sidebar (or press
Ctrl + Shift + X
). - Search for C# and install the extension by Microsoft.
5. Installing Visual Studio Community/Professional
For Windows Users:
- Download Visual Studio Community from https://visualstudio.microsoft.com/.
- Run the installer and select the .NET desktop development workload.
- Follow the installation prompts to complete the setup.
For Ubuntu Users:
Visual Studio is not available for Ubuntu, but you can use Visual Studio Code or other IDEs like Rider or MonoDevelop.
6. Creating and Running a Console Application
Using Visual Studio Code:
- Open a terminal in VSCode or your system terminal.
- Create a new console application:
dotnet new console -o MyConsoleApp
cd MyConsoleApp
- Open the project in VSCode:
code .
- Build and run the application:
dotnet run
Output:
Using Visual Studio Community/Professional:
- Open Visual Studio.
- Click on Create a new project.
- Select Console App (.NET Core) and click Next.
- Name your project (e.g.,
MyConsoleApp
) and click Create. - The IDE will create a template console application.
- Run the application by clicking the Start button or pressing
F5
.
Output:
7. Key Takeaways
- The .NET SDK is essential for developing C# applications.
- Environment variables may need to be set for the system to recognize .NET commands.
- Visual Studio Code and Visual Studio Community are powerful IDEs for C# development.
- Creating a console application is a good starting point for learning C#.
- Both Windows and Ubuntu users can set up a C# development environment.