Wednesday, 2 March 2011

Chapter 4 Hello World C# Program

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World, Welcome to C#");
        }
    }
}

using

using System;
This is another keyword which tells the compiler that we are going to use all the classes in the namespace system. Thus we have used Console.WriteLine. Point to be noted is we cannot use classes which are inside internal namespace like System.Text.

namespace

Let us first look at namespace. This is a group of related classes put under one name to keep them distinct. Lets say two different programmers decide to name their classes as SocketListen and even Foundation classes also have the same class called SocketListen, unfortunately this will cause a conflict and it is impossible to stop such incidences, plus we cannot ask anybody to change the class name because other parts might be dependent on that class. To avoid that every class is kept under a unique namespace, therefore even if we have multiple classes with the same name yet they are categorized by their namespace. So my class will be referred as helloworld.program and if their is another program in the foundation classes with the same name it might be referred as system.program.
Thos of you that have migrated from JAVA namespaces in C# is a bit different in the way they are organised on the disk. In C# namespace classes can be stored in different folders. Namespace may contain classes, delegates events and internal namespaces too.

class

A C# program can have more than one classes. Each class can contain functions,data,events etc. A more detailed explanation of class will be take n up when we discuss OOPS.

Main

Students from Java, C++, C background will recognize this function as the starting point of your program. In a technical term it is called entry point for the program. As you might have noticed there is a static keyword, this is because Main function is called by CLR without declaring an object of our class 'program'. The string[] args is called command line parameters. These are given when you execute your program from the DOS shell and want to pass some value like copy a.txt b.txt The text in Bold is called the command line parameter.
Few important points to remember are that all complete C# statement end by a (;).
Each block of code is demarcated by opening and closing curly brackets {}
Indentation while not compulsory is desirable and makes the reading more user friendly but it has no effect on compilation.
Comments can be given by using (//). You can give as many comments as you want because these are not compiled by C# compiler.

Let us make little modification to the original code
using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter your name student : ");
            string name = Console.ReadLine();
            Console.WriteLine("Hello student {0}, Welcome to the amazing world of C#",name);
        }
    }
}

In the above program we have asked for an input from the user through the statement Console.ReadLine and stored the value entered in a string type variable, then used the Console.WriteLine function to print it. Please note that the {0} is the placeholder for the argument name given at the end of the WriteLine statement.
If you have written it correctly then the Console window will ask for your name and then print the message as shown in the next figure


In the next chapter we will discuss the basic data types,variables,constant etc

No comments:

Post a Comment