December 2, 2013

Anatomy of a C Sharp program

In the previous post, we discussed about the overview of C# and .NET framework. You should keep in mind that the extent of C#, .NET etc. are enormous and extends over a large paradigm.
In this post, we will get to know about the basic skeleton of a C# program. By the end of this post, you will know the purpose of the using statement, namespace, the Main function and compiling issues.
You will be needing the code from one of the previous post: Kick-Start C# : Your first program. Don't worry, I will share the same code here as well and go on explaining.

A C# program is written on a file by the extension .cs. The dot cs extension lets us know that the compiled program is a cSharp program. Let's take a look at one of the code that we previously compiled.

using System;

namespace MyFirst
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Lets see how it works!");
            Console.ReadLine();
        }
    }
}
In this particular code, our first point of interest is line 1. The using statement is somewhat analogous to the import statement on Java or the #include statement on C/C++. It tells the C# compiler which pieces of .NET library we are going to use in the program. Here, we are telling the compiler that we need the System namespace in our program, or we are referencing some classes that are found in the System namespace. At this point, if you remove the using System; statement, you will notice that a red zigzag underline appears on line 9 and 10.
Here is a screenshot directly from my Visual Studio 2012.
Red zigzag underline pointing error
There is still a away to get rid of this error. If you type System just in front of line 9 and 10 like the following, you will notice that the error is gone. Hit F5 and see your program running.
//using System;

namespace MyFirst
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Lets see how it works!");
            System.Console.ReadLine();
        }
    }
}
The using System; statement is actually helping us to avoid writing System in front of every line. When we have a large program, you will surely find it helpful where you won't have to write the namespace before each of the classes.
Lets move our attention to line 3. The namespace MyFirst statement is clarifying to our program that, whatever classes we declare in our program, all of them will be under the namespace MyFirst. MyFirst is actually the name I have given while opening the new project. It can be any different name whichever you like. But it cannot be any name like System. You already know why! Because the C# compiler will find it confusing to deal with two namespaces with the same name!
But can we write a class that has the same name like any other built in classes? Yes we can, so long they are in different namespaces. For example, you wish to write a class named Console. Now we have seen that Console is a class under the namespace System. If you write your own Console class in a different namespace like MyFirst, you are good to go!
The next thing we would like to know about is on line 5, the class definition. When you open a new project, the Visual Studio by default makes a class for you under the name Program. You can change it if you like, but for now, we will leave it as it is. As I have previously mentioned, almost everything in C# is an object. This is no different.
On line 7, we have a very very special function, the Main function. If you are familiar with other programming languages like C, C++ or Java, you should have seen that every program has a main function. In this case, the .NET expects us to provide a Main function, no matter how many functions or lines of code you write, it will look for this special function as the start of your program, or the entry of your program. One of the differences that you will find between C# and any other language is the function notation. C# uses this convention of writing function names, the first alphabet should be in upper case. The Main function must be written like the way you are seeing it now. The void signature specifies that the method (C# and Java functions are called Methods) does not return anything. Optionally you can return an integer value. You can find more on the static and void signature in this stackoverflow post Why should main be static?

The last thing we are going to look at, the compiler issue and the .NET versions. Just above your code editor window on Visual Studio, go to Project -> <project-name> Properies. In my case, the project or the solution name is MyFirst, so its MyFirst Properties as seen on the screenshot.
Select Project Properties
A new window will be on your screen. See the highlighted part, the target framework is specified. In my case, it is .NET 4.5. So this helps you to know on what platform you are building your console application.
Project Properties
Hope you enjoyed the post. Thank you!

No comments:

Post a Comment