January 30, 2008

.Net With Example

In my territory, people know me as Mr. Example. I like to learn technology by referring some real world examples. And believe me, its fun. Whenever I see any new technology, I try to find some examples which will better express that technology. In this blog entry, I would like to elucidate .Net Framework, the 7 year old technology, which surely has altered our lives. I’ll explain it with respect to hotel business. And it’ll be fun. Yes. I mean it.

Once you go through this blog, you’ll never ever forget, ‘what is .Net framework.’ So, without wasting much time, let’s start quickly.


I presume that the reader of this blog has a basic understanding of Object Oriented Programming Concepts, like classes, objects, types, etc… Before moving ahead I would also like to tell you that framework is nothing but the methodology or guidelines set forth to execute any task. Hence Microsoft .Net Framework is nothing but a methodology which can be implemented to build applications. Now, Runtime is a host which will allow us to execute the application. To run any kind of application we require a runtime or an environment. In .Net it is Common Language Runtime or simply CLR. In proceeding example, our runtime or the host environment is a hotel or restaurant. And we’ll see different functionalities of CLR inside this hotel.

Whenever, we visit any hotel or restaurant we’re served by people, who execute different roles, like Gate Keeper – who opens the door and invite us with a pleasant smile, Reservations' Manager – who checks for reservations or seating arrangements, Captain – who takes the order, Waiter – who will serve the food, Cleaner – who cleans the dishes, etc… These different roles represents the Common Language Runtime i.e. CLR in .Net parlance. How? Let’s see.

When you are at the door, Gate Keeper welcomes you. Same ways, when you double click any application written in .Net, the .Net Framework runtime will create a process for it. That means, it will start to execute that application.

When you enter into restaurant, Reservations' Manager, with a smile on his face, asks you about reservations, if you have made any. And if not, how many people are with you? Where you would like to seat in the restaurant premises e.g. pool side, in bar, in lounge etc. Same ways, .Net framework, will allocate memory for your application to run, which depends on how many variable (or objects) you have used in your application. So, if you have created one integer object and one string objet, it’ll allocate a space for two objects in the memory. That means it’ll do the seating arrangements.
Once you take the seats, the MENU will be provided to you by Captain. This menu is like Base Class Library (BCL). Sometimes it is also called as Framework Class Library (FCL). So, as you have different food items in MENU, you have different types and structures in BCL. So, you choose items from menu and customize your own dish, likewise you choose different types (or classes) to build your application. Also, as you can add pinch of salt ‘n pepper to make the dish delicious, you can create your own types (or classes) which will allow you to fully customize your application.

Now, you are ready to order. In this case, you order some food items and Captain takes note of it. He then forwards this order to Chef. Now imagine, Captain has written the order in English and Chef doesn’t understand it. What will happen? But this is not the case. In hotel, although there can be staff members who knows various languages, there will be one common language, common code that everyone follows. So, it’s for sure, that if you ordered a Pizza, you’ll receive a Pizza. Just like that, in .Net Framework, irrespective of language chosen for development purpose (e.g. C#, VB.Net, etc…) all code is converted into Microsoft Intermediate Language (MSIL) and CLR understands this MSIL.

So, when you order Pizza, this order is taken by Captain (say in English) and forwarded to Chef in a format that he understands (say in French) and ultimately you'll receive the same output. In the same fashion, you write a code in C# which is converted to MSIL while compilation and when you execute the application, the MSIL is converted to a language which machine (on which application is running) can understand. That is 0s and 1s of machine language.

Now, it’s time for Waiter to serve the food on table. So, CLR will execute the code and you’ll be able to see output on your machine.

In small restaurants, one Waiter serves two or more tables. There can be many reasons to it, e.g. less number of staff. This might incur into interchanging of orders. Sometimes, Waiter may get confused and serve the wrong meal. This phenomenon in technology is called DLL Hell. In this case, you include some resources (like classes which are stored in dynamic link library files i.e. dll files) and if the runtime doesn’t find that resource or any link to that dll file, then it’ll give you the Blue Screen of Death (BSOD). This terminology was called DLL Hell. But in .Net this issue has been resolved. In .Net every application executes in its own world with all the required resources kept in the same folder. So, there is no chance of missing any resource.

So, when you are finished with any particular dish, Waiter or Cleaner will clean the table to accommodate any new dishes you have ordered. Likewise, in CLR, has a nice feature which is called Garbage Collector. So, when you use objects of any types (or classes) to customize your application, while execution CLR reserves memory for them. This Garbage Collector, looks for unused objects. If it finds something, it’ll clear those objects and provide more memory to your application.

As you can call a Waiter to clean the table you can call Garbage Collector to clean the memory resources. It’s pretty simple.

If during your stay in the restaurant, if you find something irritating, you call Manager and report it. In the same way, you can use Exception Handling, which will report you, if there is any error in the application execution.

So, at last, when you’re done with your lunch or dinner you are ready to leave the restaurant. Same ways, when the execution is over, CLR will terminate the application. This will also remove any objects (that you have used in your application) from memory.

Here, we have seen, what are the different features that Microsoft .Net Framework provides us with the help of Hotel example.

In next blog, I’ll come up with some new technology – with some new fundas…

Happy Coding :)