Hello World Program in C#
This C# example prints Hello World! to the console. It also introduces the basic structure of a C# program, including a namespace, class, Main method, and executable statement.
Complete C# Hello World Example
Save the following program in a file named Program.cs.
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Output of the C# Hello World Program
Hello World!
How the C# Hello World Program Works
The program contains five main parts: the using directive, namespace declaration, class declaration, Main method, and console output statement.
1. Importing the System Namespace
A using directive makes types from another namespace available without requiring their fully qualified names.
using System
In this program, the System namespace provides access to the Console class. The complete statement in the program ends with a semicolon:
using System;
Without the using System; directive, the output statement could be written with the fully qualified class name:
System.Console.WriteLine("Hello World!");
2. Declaring the HelloWorld Namespace
A namespace groups related classes, interfaces, structures, and other types. Namespaces help organize code and prevent naming conflicts between types that have the same name.
namespace HelloWorld
{
}
The namespace keyword declares a namespace named HelloWorld. The braces define the namespace body, which contains the Program class.
A namespace is optional in a small C# program, but it is commonly used in larger applications to keep source code organized.
3. Defining the Program Class
A class defines a type that can contain data and behavior. In this example, the class contains the program’s entry-point method.
class Program
{
}
The class keyword declares a class, and Program is the class name. The class body is enclosed in braces.
4. Understanding the Main Method
The Main method is the entry point of this console application. Program execution begins with the first statement inside this method.
static void Main(string[] args)
{
}
staticallows the runtime to callMainwithout first creating aProgramobject.voidindicates that this version ofMaindoes not return a value.Mainis the method name recognized as the application entry point.string[] argsstores command-line arguments passed to the program.
The command-line argument parameter is not used in this example, but it can be used to receive values supplied when the application is started.
5. Printing Hello World with Console.WriteLine
The body of a method contains statements that perform the program’s work. A C# statement usually ends with a semicolon.
Console.WriteLine("Hello World!");
Console.WriteLine() writes a value to the console and then moves the cursor to the next line. Here, the string literal "Hello World!" is passed as an argument to the method.
Run the C# Hello World Program with the .NET CLI
If the .NET SDK is installed, you can create and run a console application from a terminal.
Create a New C# Console Project
dotnet new console -n HelloWorld
cd HelloWorld
Replace the generated contents of Program.cs with the complete example shown above.
Compile and Run the C# Project
dotnet run
The .NET CLI builds the project and runs the resulting application. The console displays Hello World!.
Shorter Hello World Syntax in Modern C#
Modern C# supports top-level statements. In a console project that uses this feature, the same program can be written without explicitly declaring a namespace, class, or Main method.
Console.WriteLine("Hello World!");
The compiler generates the entry-point structure for top-level statements. The longer version remains useful for learning how traditional C# program structure works.
Common C# Hello World Errors
Missing Semicolon After a Statement
C# statements such as using System; and Console.WriteLine(...); require a terminating semicolon. A missing semicolon causes a compiler error.
Incorrect Capitalization of Main or WriteLine
C# is case-sensitive. Use Main, Console, and WriteLine with the exact capitalization shown in the example.
Unmatched Braces in the Program Structure
Each opening brace must have a matching closing brace. In this example, separate brace pairs define the namespace, class, and method bodies.
Running dotnet Commands Outside the Project Folder
Run dotnet run from the directory that contains the project’s .csproj file. Otherwise, the CLI may report that it cannot find a project to run.
C# Hello World Questions
What is the entry point of a C# console program?
In the traditional program structure, the Main method is the entry point. In a program that uses top-level statements, the compiler generates the entry point automatically.
Why is using System included in the program?
using System; allows the program to refer to Console directly. Without it, the statement can use the full name System.Console.WriteLine().
What does string[] args mean in C# Main?
string[] args is an array of strings containing command-line arguments supplied when the application starts. The parameter can remain unused when the program does not require command-line input.
What is the difference between Console.Write and Console.WriteLine?
Console.Write() prints text without automatically moving to a new line. Console.WriteLine() prints text and then adds a line break.
Can a C# Hello World program omit the namespace and class?
Yes. Modern C# console projects can use top-level statements, allowing the program to contain only Console.WriteLine("Hello World!");. The traditional structure is still valid and useful when learning classes and methods.
TutorialKart.com