C# Training - Syntax

First Program

To write and run your first C# program, follow these steps:

Step 1: Launch your preferred Integrated Development Environment (IDE). For this example, let's assume you're using Visual Studio.

Step 2: Create a new project. Go to "File" -> "New" -> "Project" in the menu. Select the C# project template that suits your needs. For a simple console application, you can choose "Console App (.NET Core)" or "Console App (.NET Framework)".

Step 3: Give your project a name and specify the location where you want to save it. Click "Create" or "OK" to create the project.

Step 4: Once the project is created, you'll see the project structure in the IDE's Solution Explorer. It typically includes a file named "Program.cs", which contains the code for your first C# program.

Step 5: Open the "Program.cs" file in the code editor. You'll see some default code generated by the project template. Replace it with the following code:

using System;

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

This code will print "Hello, World!" to the console when the program runs.

Step 6: Save the file.

Step 7: Build the project by selecting "Build" -> "Build Solution" from the menu. This will compile the code and generate the necessary executable files.

Step 8: Run the program by selecting "Debug" -> "Start Without Debugging" or pressing the "Ctrl+F5" shortcut. The console window should open and display the output "Hello, World!".

Congratulations! You have written and executed your first C# program. Feel free to experiment by modifying the code or exploring other features of the programming language.

Comments