C# Program Flow

To understand the flow of a C# program, let us begin by analyzing a very simple program in C#. Example 1 shows a program written in C#, which simply displays a message on the user’s screen.

Example 1:

001:   /* This is a simple program in C# */

002:

003:   using System;

004:   class First

005:   {

006:      public static void Main()

007:      {

008:         Console.WriteLine(“My simple program in C#”);

009:      }

010;   }

Line 001: This is a comment. Comments can be included in any part of a C# program. Here we have used a standard C/C++ style comment. The comment begins with “/*” and ends with “*/”. The comment can span multiple lines.

Line 003: This line, using System, is quite similar to the #include statement used in C/C++. The #include statement was used to include another header (source) file, so as to make the functions, present within that header file, a part of the current program. Similarly the keyword using imports the System class file and makes the methods present within it as a part of the program. But System here is known as a namespace and not as a header file. We shall discuss namespaces in a later post and see how they are different from C/C++ header files. For now, just think of namespaces as a collection of classes. The System namespace contains the classes that most applications use for interacting with the operating system. The classes that are used more often are the ones required for basic Input/Output. Note that there is a semicolon at the end of this line. All lines of code in C# must end with a semicolon, just like in C/C++. As you can see for yourself, C# has strong roots in C and C++.

Line 004: This line defines a class.

Line 006: Each class has one static void Main() function. This function is the entry point of a C# program. This means that the Main() function is the first function that is called when program execution begins. It is declared as public to make it accessible from just about anywhere in the program. The keyword public can be ignored, as by default, just as in C++, in C# also the members of a class are public. The Main() function is declared as a static member (static members will be discussed in detail later). Since in our program the Main() does not return any value its return type is declared as void. Do keep in mind the case of the keywords, all keywords on this line of code are in lower case except in case of the Main() function M is in upper case.

Line 007: Next we open the scope of the method (or function) using curly braces.

Line 008: Within the Main() function we call the WriteLine method of the Console class and pass the text “My simple program in C#” as its parameter. The WriteLine function displays text on the console or the DOS window. Note that the WriteLine method is a part of the Console class, which in turn is a part of the System namespace. If we had not specified the using System clause in Line 003, we would have had to write this line (Line 008) as:

System.Console.WriteLine(“My simple program in C#");

Using a fully qualified name to refer objects can be error prone. To ease this burden, C# provides us with the using directive, which we specified at Line 003. Also remember that you can put more than one using directive, but they all must be specified at the beginning of the program.

Line 009: The Main() function is terminated using the closing braces.

When the program is executed it will just display the message “My simple program in C#”. Though it may seem like it has not accomplished much, do not think likewise. You have just understood the basic flow of program execution in C#!

A point to note is that as mentioned earlier, C# is case sensitive! For example, the “using” is not the same as “Using”!

Leave a comment