Thursday, July 26, 2012

.Net Framework - A Layman’s View (Part -1)

Good morning folks !!!

For the next 2,3 posts i would like to concentrate on .net framework. I will not provide any text book material for you to prepare for interviews but i would like to give you an insight on what happens inside your computer from framework perspective.

Shall we start ?

All set ?

Suppose i asked you create your own programming language. What will you do?

I never done this task myself but when i asked this question to myself I'm getting following things in my mind.

First of all, i need to define rules for my language. Rules like how to declare variables, how to allow conditions loops, managing procedures/functions, classes, mathematical functions and more.

Good. I've defined my language rules and created. It is fine. Like any other language, our computer understand nothing from grammar or syntax of my language. It understands only 0's and 1's. So i need to create another program which translates my language into 0's and 1's. Your technical gurus call this program as "Compiler".

So what it should do ? It should check source code against the rules i have defined. After that it should convert the program to machine understandable language.

For example one rule in my language is,

Variables should be declared as

Declare myVariable Like Integer.

My Compiler should check whether variables declared in the program are in this pattern only and then should (can) compile the program to 0's and 1's.

If you declare your variable as

Dim myVariable As Integer.

My language compiler will show an error.

But...

But this same line will happily be compiled by vb.net compiler.

Why ?

Because vb.net compiler designed to understand variable declaration in this fashion like "Dim myVariable as Integer".

Making sense ?

Means, Vb.net compiler understands vb.net language syntax only. C# language understands only C# language. Java compiler understands only Java syntax and my compiler understands mylanguage syntax dot.

Got it?

Now my compiler is ready. It understand my rules and compiles programs written in my language. After program is compiled how it runs ? How each line is executed and how variables in memory created, manipulated and results are displayed ? It is obvious that i need to write another program to perform these tasks.Again, your super intelligent gurus call this program as "runtime". Again each runtime is created for it's own language.

While executing the program it does things like type checking, memory allocation and management, debugging and interacts with OS for various low level tasks.

Yalk...so many technical terms. Forgive me and do not remember them. Only thing you should remember is, each language requires it's own program to run the applications written for that language which technically called as "runtime"

Means, my runtime program customized for my programming language and your runtime program for your programming language.

So far so good. My language has been marketed and people are using it. No issues.

You know Gautama buddha ?

He says, to live happily we should reduce our wants.

We are great people, we never follow this suggestion and suffer. So one day, one more requirement came for my language.

I was asked to create a runtime program which should be able to execute any programming language, not only my language.

How can i do that ?

I can't.

How can i customize my runtime each and every time a new programming language is invented or created ? It is more complex task than understanding women.

I thought of solution for this problem and came up with a master stroke.

Instead of me customizing my runtime engine all the time, i will do one thing. I will create another new programming language and create runtime to execute that language. You all have compilers for your programs right ? What your compiler will do ? It will translate your programs into machine understandable language like 0'1 and 1's. Please don't do that. Ask your compiler to produce output of that new language which my runtime happily understands and executes. So what ever be the programming language you are using, after you compile, your compiler should generate my new programming language not 0'1 and 1's dot.

Do you follow ?

I will give you a generic example.

You went to another planet for a meeting which is organized for different representatives of the universe. You know English. They know their own planet language.

How do you all can communicate ?

You can't !

You need a translator.

But that translator job becomes hell if he has to translate all the languages to all the people of the universe. So he proposed a solution before one month of the meeting. He created a new language called "Talk" and circulated this language to all the universe representatives. Everyone should learn this language "Talk" to attend the meeting.

How this solves the problem ?

In the meeting, he translates all the speeches in the language "Talk" which every one understands irrespective of their native language.

Microsoft followed the same plan. They created a language called MSIL (Microsoft Intermediate Language) and all the language compilers should produce MSIL rather than 0's and 1's.They created a runtime which understands MSIL. They named it as CLR.

That means .net runtime called CLR understands only MSIL. It never know which original language was used to create that MSIL. So your VB.net compiler, C# compiler, JScript compiler, Fortran compiler and many more language compilers produce MSIL only after compilation. That's why it is named as Common Language Runtime.

Understanding something ?

Understanding CLR ?

We will know more about CLR in later posts.

By the way , what is .net Framework ?

Don't imagine something like old huge main frame machine.

As far Framework is concerned i liked one definition by the author Karl Moore in his book VB.net tutorials. He says

"You will hear  a lot of hype about .net framework in the programming world. Well, ignore all that rubbish. The truth is it's essentially a windows upgrade that works for .net programs. It runs .net applications, it handles program memory and it provides a common base of functionality for the programmers to use."

Fine ? It is as simple as that.

Ok boss. I think I'm done for now. In the next post we will see what happens inside CLR when it executes any program. How it compiles your MSIL code into machines readable form and also about the inside flow.

Before saying bye for now, just a tit bit.

I've created a simple windows project and added a super intelligent form called IAMFramework.frm as shown below. What we can do with this ?


Nothing. I just wanted to show you contents of the "bin" folder of this project. What it is showing?



It is showing nothing except for few things which are created by visual studio for its own use. At this point of time what our vb.net compiler is doing ?

It is doing nothing. It is also saying “you did’t say anything to me to do something.”

Oh..sorry compiler. I need to click on “Build” then only you will have permission to do something. I forgot.
I clicked on “Build”.

Shall we see the bin folder again?




Ooook. Now I can see .exe file which is created by the VB.net compiler.

What does this .exe file consists of?

Machine readable language!!

Correct?

Wrong!!

It consists of MSIL code.

Clear?

I’m singing off now.

No comments:

Post a Comment