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:

  1. Visit the official .NET website: https://dotnet.microsoft.com/download.
  2. Download the latest .NET SDK for Windows.
  3. Run the installer and follow the on-screen instructions.

For Ubuntu Users:

  1. Open the terminal.
  2. 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
  1. 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:

  1. Press Win + X and select System.
  2. Click on Advanced system settings.
  3. Click on Environment Variables.
  4. Under System Variables, find and select Path, then click Edit.
  5. 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:

  1. Download Visual Studio Code from https://code.visualstudio.com/.
  2. Install Visual Studio Code by following the installer instructions.
  3. Open Visual Studio Code.
  4. 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:

  1. Download Visual Studio Community from https://visualstudio.microsoft.com/.
  2. Run the installer and select the .NET desktop development workload.
  3. 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:

  1. Open a terminal in VSCode or your system terminal.
  2. Create a new console application:
dotnet new console -o MyConsoleApp
cd MyConsoleApp
  1. Open the project in VSCode:
code .
  1. Build and run the application:
dotnet run

Output:

Hello, World!

Using Visual Studio Community/Professional:

  1. Open Visual Studio.
  2. Click on Create a new project.
  3. Select Console App (.NET Core) and click Next.
  4. Name your project (e.g., MyConsoleApp) and click Create.
  5. The IDE will create a template console application.
  6. Run the application by clicking the Start button or pressing F5.

Output:

Hello, World!

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.