Hello World Program

Write a program in C sharp to display hello world explaining the entire program line by line.

1. using System;

2. public class HelloWorld

3. {

4. public static void Main()

5. {

6. II This is a single line comment

7. /* This is a

8. multiple

9. line comment */

10. Console.WriteLine("Hello World! ");

11. }

12. }

Line 1: using System;

We are using the System namespace. The point of this declaration is mostly to save ourselves time typing. Because the 'Console' object used in line 10 of the code actually belongs to the 'System' namespace, its fully qualified name is 'System.Console'. However, because in line 1 we declare that the code is using the System namespace, we can then leave off the 'System.' part of its name within the code.

Line 2: public class HelloWorld

As C# is an object-oriented language, C# programs must be placed in classes. This line declares the class to be named 'HelloWorld'.

Line 4: public static void Main()

When compiled and run, the program above will automatically run the 'Main' method declared and begun in this line. Note again C#'s case-sensitivity - the method is 'Main' rather than 'main'.

Line 3, 11 and 5, 12:

These lines uses the '{'for starting braces and '}' for closing braces of block.

Lines 6-9: Comments

('//’ uses for single line and ‘/*…………. */’ uses for multiple line comments)

These lines of the program are ignored by the compiler, being comments entered by the programmer for his own benefit.

Line 6 shows a single line comment, in which everything on the line after the two forward slashes is ignored by the compiler.

Lines 7-9 demonstrate a multi-line comment, in which everything between the opening /*and closing */ is ignored, even when it spans multiple lines. 

Line 10: Console.WriteLine ("Hello World");

The statement on this line calls the 'WriteLine' method of the Console class in the System names pace. It should be obvious how this works in the given example - it just prints out the given string to the 'Console' (on PC machines this will be a DOS prompt).

Instruction for Saving the Program

In order to run the program, it must first be saved in a file. Unlike in Java, the name of the class and the name of the file in which it is saved do not need to match up, although it does make things easier if you use this convention. In addition, you are free to choose any extension for the file, but it is usual to use the extension '.cs'.

 

 

 

 

Leave Comment

Important Topics

Title
Terminologies in C Sharp
Introduction to C Sharp
Hello World Program
C vs C Sharp
C Sharp vs C Plus Plus
C SHARP vs JAVA
Array and Types of Array
Programs - Sum of all numbers in Array
Strings and Types of Strings
Delegates in C sharp
System Collection Generic
System.Collections Classes
System.Collections.Concurrent
Dot Net Assembly
Events in C Sharp