Thursday, September 30, 2010

Writing your first c sharp program:

Writing your first c sharp program:


Introduction:

In this article we will go through some of the basics steps to get familiar with most of the things in c sharp programs. This article explains the structure of the c sharp program, how to compile and execute the c# program by using the command prompt and also it introduces you how to use the visual studio IDE to execute the projects. Let's Start With a simple 'Hello World Program'.

Hello world Program:
Open any text editor like notepad in your windows O.S and start typing the following code


namespace HelloWorld
{
    public class Hello
    {
        public static void Main()
        {
            System.Console.WriteLine("Hello World");
            System.Console.ReadLine();
        }
    }
}

Follow the steps to execute the program:

1. Save the notepad file with the extension .cs. In this case for example save your file with your class name hello.cs

2. Go to start-> All Programs-> Visual Studio 2008->visual studio Tools->Visual studio command Prompt.

3. In the visual studio command prompt change the directory to the location of your saved file.

4. And by using the command csc hello.cs compile the program.

5. Enter hello.exe to execute the program. The output will be displayed in the command prompt.

The C# Program Structure:


As we have seen a sample c# program we will try to understand the structure of the program. The basic block diagram of the c# program structure is as follows:
 

From the above diagram it is can be understood that any project created in dot net environment is nothing but an assembly. Generally assemblies are of two types. One that which is an executable file and it comes with an extension .exe. The other one is used for code reusable and its extension is .dll. The dot net framework overall consists of huge number of  assemblies by using which we can develop robust applications. The hierarchy of c# program structure goes likes this. Assembly is collection of namespaces and in turn namespaces are collections of classes, structures, methods, properties, indexers, events, delegates and so on.

To understand more clearly about the structure of the c# program we will look at the above ‘Hello World’ more precisely. The system namespace is derived from the assembly System.


namespace HelloWorld
{
    public class Hello
    {
        public static void Main()
        {
            System.Console.WriteLine("Hello World");
            System.Console.ReadLine();
               |      |      |

            namespace class  method
        }
    }
}

No comments:

Post a Comment