Friday, November 27, 2009

The .Net Architecture and .Net Framework

The Common Language Runtime (CLR)
The most important concept of the .Net Framework is the existence and functionality of the .Net Common
Language Runtime (CLR), also called .Net Runtime for short. It is a framework layer that resides above the OS and
handles the execution of all the .Net applications. Our programs don't directly communicate with the OS but go
through the CLR.
MSIL (Microsoft Intermediate Language) Code
When we compile our .Net Program using any .Net compliant language (such as C#, VB.Net or C++.Net) our
source code does not get converted into the executable binary code, but to an intermediate code known as MSIL
which is interpreted by the Common Language Runtime. MSIL is operating system and hardware independent
code. Upon program execution, this MSIL (intermediate code) is converted to binary executable code (native
code). Cross language relationships are possible as the MSIL code is similar for each .Net language.
Just In Time Compilers (JITers)
When our IL compiled code needs to be executed, the CLR invokes the JIT compiler, which compile the IL code to
native executable code (.exe or .dll) that is designed for the specific machine and OS. JITers in many ways are
different from traditional compilers as they compile the IL to native code only when desired; e.g., when a function
is called, the IL of the function's body is converted to native code just in time. So, the part of code that is not used
by that particular run is never converted to native code. If some IL code is converted to native code, then the next
time it's needed, the CLR reuses the same (already compiled) copy without re-compiling. So, if a program runs for
some time (assuming that all or most of the functions get called), then it won't have any just-in-time performance
penalty.
As JITers are aware of the specific processor and OS at runtime, they can optimize the code extremely efficiently
resulting in very robust applications. Also, since a JIT compiler knows the exact current state of executable code,
they can also optimize the code by in-lining small function calls (like replacing body of small function when its
called in a loop, saving the function call time). Although Microsoft stated that C# and .Net are not competing with
languages like C++ in efficiency and speed of execution, JITers can make your code even faster than C++ code in
some cases when the program is run over an extended period of time (like web-servers).

The Framework Class Library (FCL)
The .Net Framework provides a huge Framework (or Base) Class Library (FCL) for common, usual tasks. FCL
contains thousands of classes to provide access to Windows API and common functions like String Manipulation,
Common Data Structures, IO, Streams, Threads, Security, Network Programming, Windows Programming, Web
Programming, Data Access, etc. It is simply the largest standard library ever shipped with any development
environment or programming language. The best part of this library is they follow extremely efficient OO design
(design patterns) making their access and use very simple and predictable. You can use the classes in FCL in your
program just as you would use any other class. You can even apply inheritance and polymorphism to these classes.
The Common Language Specification (CLS)
Earlier, we used the term '.Net Compliant Language' and stated that all the .Net compliant languages can make use
of CLR and FCL. But what makes a language a '.Net compliant' language? The answer is the Common Language
Specification (CLS). Microsoft has released a small set of specifications that each language should meet to qualify
as a .Net Compliant Language. As IL is a very rich language, it is not necessary for a language to implement all the
IL functionality; rather, it merely needs to meet a small subset of CLS to qualify as a .Net compliant language. This
is the reason why so many languages (procedural and OO) are now running under the .Net umbrella. CLS basically
addresses language design issues and lays down certain standards. For instance, there shouldn't be any global
function declarations, no pointers, no multiple inheritance and things like that. The important point to note here is
that if you keep your code within the CLS boundary, your code is guaranteed to be usable in any other .Net language.
The Common Type System (CTS)
.Net also defines a Common Type System (CTS). Like CLS, CTS is also a set of standards. CTS defines the basic
data types that IL understands. Each .Net compliant language should map its data types to these standard data
types. This makes it possible for the 2 languages to communicate with each other by passing/receiving parameters
to and from each other. For example, CTS defines a type, Int32, an integral data type of 32 bits (4 bytes) which is
mapped by C# through int and VB.Net through its Integer data type.
Garbage Collection (GC)
CLR also contains the Garbage Collector (GC), which runs in a low-priority thread and checks for un-referenced,
dynamically allocated memory space. If it finds some data that is no longer referenced by any variable/reference, it
re-claims it and returns it to the OS. The presence of a standard Garbage Collector frees the programmer from
keeping track of dangling data. Ask any C++ programmer how big a relief it is!