C# Hello World – Your First C# Program: In this tutorial, we will learn to write a basic “Hello World!” program in C#. This will get you acquainted with the fundamental syntax and prerequisites of a C# program.
The Hello World! program is the most fundamental and first program when you jump into a new programming language. This basically prints the Hello World! on the output screen.
The “Hello World!” program is often the first program we see when we jump into a new language. It basically prints Hello World! on the output screen.
The purpose of this program is to get us acquainted with the fundamental syntax and prerequisites of a programming language.
In this article, you will learn-
“Hello World!” in C#
// Hello World! program namespace HelloWorld { class Hello { static void Main(string[] args) { System.Console.WriteLine("Hello World!"); } } }
At the point when you run the program, the output will be:
Hello World!
How the “Hello World!” program in C# works?
Let’s break down the program line by line.
- //Hello World! Program
//indicates the start of a comment in C#. Comments are not executed by the C# compiler.
They are planned for the developers to all the more likely comprehend a piece of code. To learn more about comments in C#, visit C# comments.
- namespace HelloWorld{…}
The namespace catchphrase is used to define our own namespace. Here we are creating a namespace called HelloWorld.
Simply consider namespace as a container which comprises of classes, techniques and other namespaces.
3. class Hello{…}
The above statement creates a class named – Helloin C#. Since, C# is an object oriented programming language, creating a class is required for the program’s execution.
4. static void Main(string[] args){…}
Main() is a strategy for class Hello. The execution of each C# program begins from the Main() technique. So it is mandatory for a C# program to have a Main() strategy.
The signature/syntax of the Main() technique is:
static void Main(string[] args) { ... }
We’ll learn more about methods in the later chapters.
5. System.Console.WriteLine(“Hello World!”);
Until further notice, simply recollect that this is the piece of code that prints Hello World! to the output screen.You’ll learn more about how it works in the later chapters.
Alternative Hello World! implementation
Here’s an alternative method to write the “Hello World!” program.
// Hello World! program using System; namespace HelloWorld { class Hello { static void Main(string[] args) { Console.WriteLine("Hello World!"); } } }
Notice for this situation, we’ve written using System; toward the beginning of the program. By using this, we would now be able to replace
System.Console.WriteLine("Hello World!");
With
Console.WriteLine("Hello World!");
This is a convenience we’ll use in our later chapters too.
Things to recollect from this tutorial
• Every C# program should have a class definition.
• The execution of program starts from the Main() method.
• Main() method should be inside a class definition.
This is only a basic program for acquainting C# with a novice. In the event that you didn’t comprehend certain things in this tutorial, that is alright (even I didn’t when I began). As we proceed onward with this tutorial series, everything will start to make sense.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.