Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#
Article

Traditional "Hello World" Program Using Different Approaches. PART-I

Rate me:
Please Sign up or sign in to vote.
3.13/5 (15 votes)
30 Sep 20013 min read 112.9K   17   13
Very basic understanding of the first C# program.

Introduction

You may think that the title of this article looks something odd and too lengthy. So, before moving on to the point, let me first explain to you the title.

By "Traditional Hello World", I mean that when we try to learn a new language, as the tradition goes on, we always introduce the language with a "Hello World" program. This is just a simple program which displays "Hello World" on the console screen. (PART-I)

By "Using Different Approaches", I mean that I would explain the "Hello World" program using various methods. So, the reader gets a basic and clear understanding of Object Oriented Programming using C#. (PART-II)

So folks, without breaking the tradition, let's move on to our program:

C#
/*1*/      using System;
/*2*/         public class world
/*3*/            {
/*4*/            public static void Main()
/*5*/               {
/*6*/                 Console.WriteLine("Hello World");
/*7*/                }
/*8*/             }

You can type this code into Notepad or any of your favorite editor and save the file as helloworld.cs (you can save it with any name but the extension should be .cs). After saving the file, go to console screen (i.e., the black colored DOS prompt Window) and type csc helloworld.cs and press Enter.

The program would successfully compile, provided that there is .NET platform installed on your PC and the program is typed correctly without any mistakes.

This would generate a file as helloworld.exe which when run will give you "Hello World" on the screen. Let us now examine the different parts of our Helloworld program:

LINE: 1

C#
using System;
  • using: Using is very similar in concept to Java's import keyword. Also, if you love C/C++ programming, then you may assume it as include keyword.
  • System: System is the most fundamental class in which every other class is defined.
  • Semicolon (;) is used to end the statement.

LINE: 2

C#
public class world
  • public: This keyword denotes that the class is visible to everyone.
  • class: This class as the name suggests denotes that this is a class.
  • world: It is the name of the class (user defined, can be any name).

LINE: 3

C#
{

Starting curly braces for class world.

LINE: 4

C#
public static void Main()

This is the main entry point of this program.

  • public: I think there is no need to explain this access modifier again.
  • static: static indicates that the method is a class method and can be called without making an instance of the class first.
  • void: This is the return type of the Main method. It means that no value is returned for this method. This return type denotes the error level.
  • Main(): It is the name of the 'main method' which is Main (Main with capital M).

LINE: 5

C#
{

Starting curly brace for method Main().

LINE: 6

C#
Console.WriteLine("Hello World")

At last, I am on the actual line of the code which displays the traditional holy word (FOR PYSCHOs) "Hello World".

  • Console: Here Console is a class just like world in this example, and
  • WriteLine: It is a method of class Console and need not be initialized. I.e., it is a static method.

And "HelloWorld" is the string that we pass in the WriteLine method, so that it would be printed on the Console screen, i.e., the black colored DOS prompt.

LINE: 7

C#
}

Closing curly brace for method Main().

LINE: 8

C#
}

Closing curly brace for class world.

So folks, if you want to thank me, then what would you do? Let me guess. You would change line 6 as:

C#
Console.WriteLine("Thank You Nemesh");

I have also uploaded the second part of this article Traditional Hello World Program using different approaches. PART -II.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThank you. LOW COST DEVELOPMENT IN INDIA. www.xtremeheights.com Pin
nemesh4-Nov-04 18:56
nemesh4-Nov-04 18:56 
GeneralRe: Thank you. LOW COST DEVELOPMENT IN INDIA. www.xtremeheights.com [modified] Pin
ggokul3-Apr-07 19:48
ggokul3-Apr-07 19:48 
GeneralNew to programming!!! Pin
Anonymous28-Aug-02 15:26
Anonymous28-Aug-02 15:26 
Hello,

I am new to programming in the sence i have read the word 'PROGRAMMING" can anyone offer advice on how i can learn to program etc???? Please helpEek! | :eek:
GeneralRe: New to programming!!! Pin
Ray Cassick28-Aug-02 17:23
Ray Cassick28-Aug-02 17:23 
GeneralToo good Pin
4-Feb-02 17:27
suss4-Feb-02 17:27 
Generalnice work :-) Pin
Nish Nishant11-Oct-01 17:12
sitebuilderNish Nishant11-Oct-01 17:12 
GeneralHello World Pin
addicted_to_cpp7-Oct-01 12:50
addicted_to_cpp7-Oct-01 12:50 
GeneralRe: Hello World Pin
7-Oct-01 22:50
suss7-Oct-01 22:50 
GeneralGoing back to school Pin
27-Sep-01 9:55
suss27-Sep-01 9:55 
GeneralRe: Going back to school Pin
27-Sep-01 10:12
suss27-Sep-01 10:12 
GeneralRe: Going back to school Pin
28-Sep-01 1:03
suss28-Sep-01 1:03 
GeneralRe: Going back to school Pin
Uwe Keim1-Oct-01 2:30
sitebuilderUwe Keim1-Oct-01 2:30 
GeneralRe: Going back to school Pin
NormDroid1-Oct-01 2:55
professionalNormDroid1-Oct-01 2:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.