C# Hello World Program (Using Main Method and Console.WriteLine)
This tutorial walks you through writing the Hello World program in C#. If you're new to C# or .NET, this is the perfect place to begin.
Whether you're searching for:
-
Hello World for C#
-
C# Console Application Hello World
-
C# Basic Program Example
This article is designed for you!
Introduction
The Hello World program in C# introduces basic syntax including namespaces, classes, the Main()
method, and the Console.WriteLine()
function. It’s a foundational step for learning the language.
Basic C# Hello World Program
// HELLO WORLD PROGRAM IN C# USING MAIN METHOD
// C# Console.WriteLine() function
// C# static Main method
using System;
class W3CW {
// It's the main function of the program
static public void Main(string[] args) {
// Step-1 Display a label message
Console.WriteLine("C# HELLO WORLD PROGRAM");
// Step-2 Print the actual Hello World message
Console.WriteLine("HELLO, WORLD!");
}
}
Output
C# HELLO WORLD PROGRAM
HELLO, WORLD!
Using a Function
// C# HELLO WORLD PROGRAM USING A SEPARATE FUNCTION
using System;
class W3CW {
// This function prints the hello world messages
static void PrintMessage() {
Console.WriteLine("C# HELLO WORLD PROGRAM");
Console.WriteLine("HELLO, WORLD!");
}
static public void Main(string[] args) {
// Call the function to print the message
PrintMessage();
}
}
Using a Loop
// C# HELLO WORLD PROGRAM USING FOR LOOP
using System;
class W3CW {
static public void Main(string[] args) {
// Step-1 Label message
Console.WriteLine("C# HELLO WORLD PROGRAM");
// Step-2 Repeat Hello World 3 times using loop
for (int i = 0; i < 3; i++) {
Console.WriteLine("HELLO, WORLD!");
}
}
}
Common Mistakes to Avoid
-
Forgetting to include
using System;
-
Misspelling
Main
method or incorrect access modifiers. -
Omitting
static
keyword inMain()
method. -
Forgetting semicolons after statements.
FAQs
Q: Do I need Visual Studio to write C#?
A: Not necessarily. You can also use VS Code, JetBrains Rider, or the dotnet
CLI.
Q: Why is Main()
static in C#?
A: Because it is the entry point and needs to be called without creating an object.
Q: What is Console.WriteLine()
?
A: It is a built-in method to print output on the console.