Open In App

C# Hello World

Last Updated : 17 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Hello World Program is the most basic program when we dive into a new programming language. This simply prints "Hello World!" on the console. In C#, a basic program consists of the following:

  • A Namespace Declaration
  • Class Declaration & Definition
  • Class Members(like variables, methods, etc.)
  • Main Method
  • Statements or Expressions

Example: Hello World program in C#.

C#
// C# program to print Hello World! 
using System; 

// namespace declaration 
namespace HelloWorldApp { 
    
    // Class declaration 
    class Geeks { 
        
        // Main Method 
        static void Main(string[] args) { 
            
            // statement 
            // printing Hello World! 
            Console.WriteLine("Hello World!"); 
            
            // To prevent the screen from 
            // running and closing quickly 
            Console.ReadKey(); 
        } 
    } 
} 

Output
Hello World!

Explanation:

  • System: The System namespace contains core functionalities like Console, Math, and other essential classes.
  • using System;: This is a directive that allows access to these built-in classes without needing to specify System. before them.
  • namespace HelloWorldApp: Here namespace is the keyword which is used to define the namespace. HelloWorldApp is the user-defined name given to the namespace. For more details, refer to C# | Namespaces
  • class Geeks: Here class is the keyword which is used for the declaration of classes. Geeks is the user-defined name of the class.
  • static void Main(string[] args): Here static keyword tells us that this method is accessible without instantiating the class. void keyword tells that this method will not return anything. The Main() method is the entry point of our application. In our program, the Main() method specifies its behaviour with the statement Console.WriteLine(“Hello World!”);.
  • Console.WriteLine(): Here WriteLine() is a method of the Console class defined in the System namespace.
  • Console.ReadKey(): This is for the VS.NET Users. This makes the program wait for a key press and prevents the screen from running and closing quickly.

Executing C# Program

There are generally three ways to compile and execute a C# program.

  • Using Online Compiler: There are various online IDEs, which can be used to run C# programs without installing. This compiler runs the C# program in a virtual machine and shows the related output.
  • Using IDE(Integrated Development Environment): We can use different IDE Microsoft has provided an IDE (Integrated Development Environment) tool named Visual Studio to develop applications using different programming languages such as C#. It comes up with many powerful integrated tools.
  • Using Command Line: We can use the command line to run the C# program. There are some requirements to run the C# program locally first download the .NET SDK on the local system the .NET SDK is important to install because it creates a common run time environment for executing the C# program.

Now follow the below steps:

Step 1: Open a command prompt and create an empty .NET project using the following command:

dotnet new console -o <Project-Name>

In the <Project-Name> we can provide the name of the project which we want to build in this example as shown below. Here, we use the project name as HelloWorld. This command will create an empty project template with all the necessary packages required to run the .NET project.

DotNETProjectCreation



This is the complete folder structure which is created using the above command.

FolderStructure



The Program.cs is the entry point in this file the C# code is written we can not directly open this file but we can open it in any IDE like Visual Studio or Visual Studio Code this is the default code which is written inside the program.cs file as shown in the below image

FolderStructure

This is a simple program we can run it using the following command mentioned in the below steps.


Step 2: Now we need to build the project using the command mentioned below.

Navigate to the project directory:

cd HelloWorld

Build the project using:

dotnet build

dotNETBuildCMD



Step 3: Now to see the output run the command mentioned below.

dotnet run

DotnetRunCMD

This will execute the default Program.cs file and display output in the console.


Next Article
Article Tags :

Similar Reads