Hello World in C# is usually the first program you write when learning the language. It is small, but it introduces the basic structure of a C# program, the role of the Main() method, the Console.WriteLine() statement, namespaces, classes, and how a C# file is compiled and executed through .NET. Once this example is clear, moving into variables, data types, operators, methods, and object-oriented programming becomes much easier.
C# is commonly used with the .NET SDK, so a Hello World program is not only about writing one line of code. It also helps you understand how to create a project, where the source file lives, how the build command works, and what happens when you run the program from the terminal or an IDE.
What Is Hello World in C#?
Hello World in C# is a simple program that prints a message on the console screen. The output is usually:
Hello, World!
This program is useful because it confirms that your C# environment is installed correctly and that you can create, build, and run a basic .NET application.
Traditional Hello World Program in C#
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}
This is the classic form of a C# console program. It shows the structure explicitly, which makes it a good version for beginners who want to understand what is happening behind the scenes.
Output
Hello, World!
Explanation of the Program
| Part | Meaning |
|---|---|
using System; | Imports the System namespace so we can use Console directly. |
class Program | Defines a class named Program. |
static void Main() | Defines the entry point where program execution begins. |
Console.WriteLine() | Prints text on the console and moves to the next line. |
"Hello, World!" | A string literal printed by the program. |
using System
The line using System; imports the System namespace. The Console class is part of this namespace. Without this using directive, you can still write the program, but you would need to use the fully qualified name.
System.Console.WriteLine("Hello, World!");
Both forms are valid. Beginners usually use using System; because it keeps the code shorter and easier to read.
The Program Class
In C#, code is usually organized inside classes. A class is a blueprint that can contain methods, fields, properties, and other members. In a simple console program, the class is often named Program, but the name can be different as long as the entry point is valid.
The Main Method
The Main() method is the starting point of many C# console applications. When the program runs, execution begins from this method. The keyword static means the method belongs to the class itself and can be called without creating an object of the class. The keyword void means the method does not return a value.
C# also supports variations of the Main method, such as returning an integer or accepting command-line arguments, but the simple static void Main() form is enough for a beginner Hello World program.
Console.WriteLine in C#
Console.WriteLine() is used to display output on the console. The text inside double quotes is passed as an argument to the method. After printing the text, WriteLine() moves the cursor to the next line.
Console.WriteLine("Welcome to C#");
Console.WriteLine("This is a console program");
The output will appear on two separate lines because each call to WriteLine() ends with a newline.
Console.Write vs Console.WriteLine
| Method | Behavior |
|---|---|
Console.Write() | Prints output without moving to a new line. |
Console.WriteLine() | Prints output and then moves to a new line. |
Console.Write("Hello ");
Console.Write("World");
This prints both words on the same line. Understanding this small difference helps when formatting console output later.
Modern Hello World Using Top-Level Statements
Modern C# also supports top-level statements. This allows a small console program to be written without explicitly declaring the Program class and Main() method.
Console.WriteLine("Hello, World!");
This version is shorter, and many new .NET templates use it by default. However, the compiler still creates an entry point behind the scenes. For learning, it is useful to understand both the traditional form and the modern top-level form.
Creating a Hello World Project Using .NET CLI
If the .NET SDK is installed, you can create and run a console project from the command line.
dotnet new console -n HelloWorldApp
cd HelloWorldApp
dotnet run
The first command creates a new console application. The second command moves into the project folder. The third command builds and runs the program.
Important Files in a C# Hello World Project
| File | Purpose |
|---|---|
Program.cs | Contains the C# source code. |
.csproj | Project file that stores target framework and build settings. |
bin | Build output folder created after compilation. |
obj | Intermediate build folder used by the compiler and tooling. |
Beginners often focus only on Program.cs, but the project file is also important because it tells .NET how to build the application.
Top-Level Statements vs Traditional Main
Newer C# project templates often use top-level statements because they reduce boilerplate for small programs. This is useful for beginners because the first file looks clean. However, the traditional Main() method is still important because you will see it in older projects, interview examples, textbooks, and many structured applications. The short form is easier to start with, while the traditional form makes the execution model more visible.
| Style | Best For | What You See |
|---|---|---|
| Top-level statements | Small programs and quick learning | Only the executable statements |
| Traditional Main method | Understanding structure and older code | Class, method, braces, and entry point |
Build vs Run in C#
When you use dotnet build, the .NET SDK checks the project, compiles the code, and creates build output. When you use dotnet run, it builds the project if needed and then executes it. For beginners, dotnet run is convenient because it performs both steps in one command, but knowing the difference helps when you start debugging build errors separately from runtime errors.
dotnet build
dotnet run
Main Method Variations
The Hello World example usually uses static void Main(), but C# supports more than one valid entry-point signature. The program can return an integer status code or accept command-line arguments. These forms become useful when writing command-line tools or automation programs.
static int Main(string[] args)
{
Console.WriteLine("Hello from command line app");
return 0;
}
The args array can store values passed from the terminal, and the returned integer can indicate whether the program completed successfully. A return value of 0 usually means success.
Using Comments in the First Program
You can add comments to explain what the program is doing. Comments are ignored by the compiler, but they are useful while learning.
// This line prints text on the console
Console.WriteLine("Hello, World!");
Comments should explain intent, not repeat every obvious symbol. In beginner programs, they are helpful for remembering what each line means. In larger programs, they should be used carefully so the code stays clean.
Common Errors in Hello World Program
- Missing semicolon after
Console.WriteLine(). - Typing
consoleinstead ofConsole, because C# is case-sensitive. - Forgetting double quotes around the text.
- Running the command from the wrong folder.
- Not installing the .NET SDK before using
dotnetcommands.
C# Is Case-Sensitive
C# treats uppercase and lowercase letters as different. That means Console, console, and CONSOLE are not the same. The correct class name is Console with an uppercase C.
Adding More Output
Console.WriteLine("Hello, World!");
Console.WriteLine("I am learning C# programming.");
Console.WriteLine("This is my first console application.");
Once the basic program works, you can add more output statements and observe how the console displays each line. This is a simple but useful way to test syntax and understand program execution flow.
Changing the Message
After the first program works, the easiest experiment is to change the message inside the double quotes. This helps you understand that the compiler is not printing a fixed built-in message. It prints the string you provide to Console.WriteLine(). You can write your name, a sentence, numbers as text, or multiple lines. Small experiments like this are useful because they build confidence with editing, saving, running, and observing output.
What You Learn from Hello World in C#
- How to write a basic C# source file.
- How program execution starts from an entry point.
- How to print output using the Console class.
- How the .NET CLI creates and runs a project.
- How C# syntax uses braces, parentheses, double quotes, and semicolons.
- How modern C# top-level statements simplify small programs.
Also remember that every C# learning step after this will still follow the same basic workflow: edit the source file, build the project, run the application, inspect the output, and fix any compiler or runtime errors carefully.
Can I write Hello World in one line in C#?
Yes. Modern C# allows top-level statements, so a simple program can be written as Console.WriteLine("Hello, World!");.
Do I need Visual Studio for Hello World in C#?
No. You can use Visual Studio, Visual Studio Code, Rider, or only the .NET CLI from a terminal.
Why does C# use semicolons?
Semicolons mark the end of many statements in C#. If a required semicolon is missing, the compiler usually reports a syntax error.
Best Practices for Your First C# Program
- Type the code yourself instead of only copying it.
- Run the program after each small change.
- Read compiler errors carefully instead of deleting random code.
- Use meaningful project names and file names.
- Practice both the traditional
Main()version and the top-level statement version.
Hello World in C# may be a tiny program, but it introduces the basic path of C# development: write code, build it, run it, read the output, and fix errors. That same cycle is used for every larger C# application you will build later.