C# Write your first program


To write your first C# program, you will follow a series of simple steps. C# is a high-level, object-oriented programming language, and its syntax is designed to be simple and readable.

Here's a step-by-step guide to help you write and run your first C# program using either the .NET CLI (Command Line Interface) or an IDE like Visual Studio or Visual Studio Code.


Option 1: Using .NET CLI (Command Line Interface)

The .NET CLI allows you to create and run C# projects directly from the terminal or command prompt.

Step 1: Install .NET SDK

  • Before writing your first C# program, ensure that the .NET SDK is installed on your system. You can download it from the official Microsoft .NET website.
  • After installation, verify it by running:
    dotnet --version
    This should return the version number of your installed SDK.

Step 2: Create a New Console Application

  1. Open a terminal or command prompt.

  2. Navigate to a folder where you want to create your project, or create a new folder:

    mkdir MyFirstApp cd MyFirstApp
  3. Run the following command to create a new console application:

    dotnet new console -n MyFirstApp
  4. This will create a project named MyFirstApp. Navigate into this folder:

    cd MyFirstApp

Step 3: View the Default C# Code

Inside the project folder, open the Program.cs file. It contains basic code that prints a message to the console. The content should look like this:

using System; class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); } }

This code includes:

  • using System;: A directive that allows access to classes in the System namespace.
  • class Program: A class definition, where the program’s code resides.
  • static void Main(string[] args): The Main method is the entry point of the application.
  • Console.WriteLine("Hello, World!");: A line of code that prints "Hello, World!" to the console.

Step 4: Run the Program

Now that your program is created, you can run it using:

dotnet run

You should see the output:

Hello, World!

Option 2: Using Visual Studio

Visual Studio is a feature-rich Integrated Development Environment (IDE) that simplifies C# development.

Step 1: Install Visual Studio

  • Download and install Visual Studio from https://visualstudio.microsoft.com/.
  • During installation, ensure you select the .NET desktop development workload to enable C# project creation.

Step 2: Create a New Console Project

  1. Open Visual Studio.
  2. On the start screen, click Create a new project.
  3. From the list of templates, select Console App.
  4. Choose a project name, location, and click Create.

Step 3: View the Default Code

Visual Studio will create a new project with a Program.cs file containing the default code:

using System; class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); } }

Step 4: Run the Program

To run the program, simply click the Start button (a green triangle icon) or press F5. Visual Studio will compile and run the program, and you will see the output in the console window:

Hello, World!

Option 3: Using Visual Studio Code

Visual Studio Code is a lightweight code editor that can be configured to work with C#.

Step 1: Install Visual Studio Code

Step 2: Install C# Extension

  • Open Visual Studio Code.
  • In the Extensions view (Ctrl+Shift+X), search for the C# extension (by Microsoft) and install it. This extension provides IntelliSense, debugging, and project management features.

Step 3: Create a New Console Application

  1. Open a terminal in VS Code (Ctrl+ ``).

  2. Run the following command to create a new console application:

    dotnet new console -n MyFirstApp cd MyFirstApp
  3. Open the folder in Visual Studio Code:

    • Go to File -> Open Folder.
    • Select the MyFirstApp folder.

Step 4: View and Modify Code

You will see the default Program.cs file with the same "Hello, World!" code.

Step 5: Run the Program

You can run the program in the terminal using:

dotnet run

Alternatively, you can set up the debugger to run the program by pressing F5.


Understanding the Basic C# Code

using System;

This line tells the compiler that you want to use classes from the System namespace, which contains basic system functionalities like Console.

class Program

This defines a class named Program. In C#, all code is encapsulated in classes.

static void Main(string[] args)

  • The Main method is the entry point of every C# application. It is where the program starts execution.
  • static means this method belongs to the class itself, not an instance of the class.
  • void means this method does not return any value.
  • string[] args is an array of strings that can store command-line arguments passed to the program.

Console.WriteLine("Hello, World!");

  • Console is a class in the System namespace used to perform input and output operations.
  • WriteLine is a method that prints text to the console and moves the cursor to the next line.

Modifying the Program

Let’s modify the program to ask for the user’s name and greet them.

Updated Code:

using System; class Program { static void Main(string[] args) { Console.Write("Enter your name: "); string name = Console.ReadLine(); Console.WriteLine($"Hello, {name}!"); } }

Explanation:

  • Console.Write: Prints a message without moving to the next line.
  • Console.ReadLine: Reads a line of text input from the user.
  • $"Hello, {name}!": String interpolation to include the name variable inside the string.

Output:

Enter your name: John Hello, John!