Writing and Running Your First C# Program

Introduction

In this chapter, we will learn how to create and run your first C# program: Hello World. This simple program will introduce you to the basic structure of a C# application and how to compile and run it using different development environments.

Creating Your First C# Project

Using the .NET CLI

The .NET Command Line Interface (CLI) is used for creating, building, and running .NET applications. Follow these steps to create and run your first C# program using the .NET CLI:

  1. Open a Command Prompt or Terminal.

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

    dotnet new console -n MyFirstApp
    

    This command creates a new directory called MyFirstApp with a basic C# console application.

  3. Navigate to the Project Directory:

    cd MyFirstApp
    
  4. Run the Project:

    dotnet run
    

    This command compiles and runs the application, displaying "Hello World!" in the console.

Using Visual Studio

Visual Studio is a powerful Integrated Development Environment (IDE) for C# development. Follow these steps to create and run your first C# program using Visual Studio:

  1. Open Visual Studio.

  2. Create a New Project:

    • Click on "Create a new project".
    • Select "Console App (.NET Core)" and click "Next".
    • Name your project "MyFirstApp" and choose a location to save it.
    • Click "Create".
  3. Write Your Code:

    • Visual Studio automatically generates a basic console application. Open the Program.cs file, which contains the following code:
    using System;
    
    namespace MyFirstApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello, World!");
            }
        }
    }
    
  4. Run the Project:

    • Click the green "Start" button or press F5 to build and run the application. The output window will display "Hello, World!".

Using Visual Studio Code

Visual Studio Code is a lightweight, cross-platform code editor that supports C# development through extensions. Follow these steps to create and run your first C# program using Visual Studio Code:

  1. Open Visual Studio Code.

  2. Open the Terminal: Go to View > Terminal or press Ctrl+` to open the integrated terminal.

  3. Create a New Project:

    • In the terminal, run:

      dotnet new console -n MyFirstApp
      
  4. Open the Project:

    • Open the project folder in Visual Studio Code by going to File > Open Folder and selecting the MyFirstApp directory.
  5. Write Your Code:

    • Open the Program.cs file, which contains the following code:
    using System;
    
    namespace MyFirstApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello, World!");
            }
        }
    }
    
  6. Run the Project:

    • In the terminal, run:

      dotnet run
      

    This command compiles and runs the application, displaying "Hello, World!" in the terminal.

Understanding the Code

Here’s a breakdown of the basic C# program:

using System;

namespace MyFirstApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}
  • using System;: This statement includes the System namespace, which contains fundamental classes for input and output operations.
  • namespace MyFirstApp: A namespace is used to organize code and prevent naming conflicts.
  • class Program: A class is a blueprint for creating objects. The Program class contains the code for our application.
  • static void Main(string[] args): The Main method is the entry point of the application. It is where the program starts executing.
  • Console.WriteLine("Hello, World!");: This line prints the string "Hello, World!" to the console.

Conclusion

Congratulations! You have successfully written and run your first C# program. This simple "Hello, World!" application introduces you to the basic structure of a C# program and how to use different development environments to create and run C# applications. In the next chapters, we will dive deeper into the fundamentals of C# programming, including variables, data types, control flow, and more.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top