Hello World program in C#


Hello World program in C# Programming Language:

using System;
- We must write using System; in the beginning of the sources code because most of the C# fundamental constructs like structures, classes, interfaces, enumerators, etc. are wrapped within System namespace.

namespace HelloWorld { }
- It tells to the compiler that the underlying class Program belongs to the namespace HelloWorld.

Note: namespace is meant to wrap one or more structures, classes, interfaces, enumerators etc.

{} : (pair of flower brackets)
- Pair of flower brackets indicate block of code. Opening flower bracket indicates beginning of the specific construct and closing flower bracket indicates end of the specific construct.

class Program { }
- C# is an object oriented programming language; hence to execute C# code at least one class must be defined.

Note: A class containing Main() method is called main class.

static void Main(string[] args) { }

Main() :
- Main() is a method. It is the entry point where the execution begins

void: is a keyword
- It indicates Main() method does not return anything back.

static: is a keyword
There are 2 reasons for Main() method to be static
1. static members are executed before non static members
2. CLR does not need to create an instance for executing Main() method

string[] args:
- string[] args is an argument to the Main() method; which indicates args is an array of type string. It is used to receive values of type string from the command prompt.

Console.WriteLine("Hello World");
- WriteLine is a method present inside Console class.
- WriteLine method displays a given value to the console window and implicitly moves the cursor to the Nextline so that any subsequent output gets displayed on the new fresh line.

Note: Sequence of characters enclosed in between pair of double quotations is called a string in C#

- ReadKey() is a method present inside Console class. It makes the console to wait for a keyboard input.

Note: C# statements must be terminated by semicolon (;) symbol.