On this page
Hello, World! Console Application in C#
In this tutorial, we’ll walk through the steps to create a simple “Hello, World!” console application in C# using .NET Core CLI (dotnet).
Prerequisites
Before you begin, make sure you have the following installed:
- .NET SDK for your operating system
Step 1: Open a Terminal (Command Prompt)
- Windows: Open Command Prompt (
cmd) or PowerShell. - Linux/macOS: Open your terminal application.
Step 2: Create a New Console Application
- Navigate to the directory where you want to create your project.
cd path/to/your/directory
- Use the dotnet new command to create a new console application:
dotnet new console -n HelloWorld
dotnet new console: Creates a new console application.-n HelloWorld: Specifies the name of the project as HelloWorld. This command creates a folder named HelloWorld with the basic structure and files for a console application.
Step 3: Navigate to the Project Directory
On Windows
- Change directory to
HelloWorld:
cd HelloWorld
- List the files in the directory to confirm the project files are created:
dir # Use `dir` command
On Linux/macOS
- Change directory to
HelloWorld:
cd HelloWorld
- List the files in the directory to confirm the project files are created:
ls # On macOS and Linux use `ls` command
Step 4: Open and Modify Program.cs
- Open Program.cs in your preferred text editor or integrated development environment (IDE).
code Program.cs # Opens in Visual Studio Code, replace with your editor command
- Replace the content of
Program.cswith the following C# code:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
Explanation of the code:
using System;: Imports the System namespace, which contains fundamental types and classes.namespace HelloWorld { ... }: Defines a namespace named HelloWorld to organize your code.class Program { ... }: Defines a class named Program.static void Main(string[] args) { ... }: The entry point of the program.Mainis a static method where program execution begins. It accepts an array of strings (args) as parameters.Console.WriteLine("Hello, World!");: Prints “Hello, World!” to the console.
Step 5: Run the Application
- Back in the terminal, build and run the application:
dotnet run
You should see the output:
Hello, World!
Top-Level Statements (C# 9+ / .NET 6+)
Modern templates use simplified syntax:
// Program.cs — no class or Main required
Console.WriteLine("Hello, World!");
The compiler generates the Main method automatically.
Project Structure
HelloWorld/
├── HelloWorld.csproj
├── Program.cs
└── obj/ # build artifacts (gitignored)
The .csproj file defines target framework and dependencies:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
</Project>
Build vs Run
dotnet build # compiles to bin/Debug/net8.0/
dotnet run # builds (if needed) and executes
dotnet build -c Release # optimized release build
Adding NuGet Packages
dotnet add package Newtonsoft.Json
dotnet restore
Packages download to a local cache and are referenced in the .csproj.
Next Steps
- Modify output to print your name and today’s date.
- Accept a command-line argument:
dotnet run -- Alice. - Create a new class in a separate file and call it from
Program.cs. - Explore
dotnet new listfor project templates (web, class library, xunit).