Monday, July 14, 2008

.NET Framework Overview

The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, .NET framework class libraries, and user and program interface.



Common Language Runtime (CLR)

Many different languages and platforms provide a runtime, and the .NET Framework is no exception. You will find, however, that this runtime is quite different from most.

The Common Language Runtime (CLR) in the .NET Framework manages the execution of the code and provides access to a variety of services that will make the development process easier.

The CLR has been developed to be far superior to previous runtimes, such as the VB runtime, by attaining the following:

- Cross-language integration
- Code access security
- Object lifetime management
- Debugging and profiling support

Code that is compiled and targeted to the CLR is known as managed code. Managed code provides metadata that is needed for the CLR to provide the services of multilanguage support, code security, object lifetime management, and memory management.


Common Type System (CTS)

CTS defines all of the basic types that can be used in the .NET Framework and the operations performed on those type. All this time we have been talking about language interoperability, and .NET Class Framework. None of this is possible without all the language sharing the same data types. What this means is that an int should mean the same in VB, VC++, C# and all other .NET compliant languages. This is achieved through introduction of Common Type System (CTS).

Common Language Specification (CLS)

CLS is the collection of the rules and constraints that every language (that seeks to achieve .NET compatibility) must follow. It is a subsection of CTS and it specifies how it shares and extends one another libraries.
Microsoft Intermediate Language (MSIL)
A .NET programming language (C#, VB.NET, J# etc.) does not compile into executable code; instead it compiles into an intermediate code called Microsoft Intermediate Language (MSIL). As a programmer one need not worry about the syntax of MSIL - since our source code in automatically converted to MSIL. The MSIL code is then send to the CLR (Common Language Runtime) that converts the code to machine language, which is, then run on the host machine. MSIL is similar to Java Byte code. MSIL is the CPU-independent instruction set into which .NET Framework programs are compiled. It contains instructions for loading, storing, initializing, and calling methods on objects. Combined with metadata and the common type system, MSIL allows for true cross- language integration Prior to execution, MSIL is converted to machine code. It is not interpreted.


Jit compilation

The .NET Framework contains one or more JIT compilers that compile your IL code down to machine code, or code that is CPU-specific. This is done when the application is executed for the first time.

You will notice this process after you build your first ASP.NET page. After you build any ASP.NET page, you compile the page down to IL. When you go to the browser and call the page by typing its URL in the address bar, you notice a slight pause of a few seconds as the computer seems to think about what it is doing. It is actually calling this IL code and converting it with a JIT compiler to machine code. This happens only the first time that someone requests the page. After the first time, you can hit F5 to refresh the page, and the page is immediately executed. The page has already been converted to machine code and is now stored in memory. The CLR knows the JIT compiler has already compiled the page.Therefore, it gets the output of the page from memory. If you later make a change to your ASP.NET page, recompile, and then run the page again, CLR detects that there was a change
to the original file. It uses the JIT compiler once again to compile the IL code down to machine code.

Namespaces in .NET

A Namespace is a group of related classes. It is a good practice to group related classes into a namespace when you create a class library. The main advantage of using namespaces is, to avoid conflicts when you have multiple classes with the same name. For example, .NET comes with a class called 'Form'. Suppose you also write another class called 'Form' in your application and if you refer to the class 'Form', how does the compiler know which Form you are referring to? Here is the importance of 'namespace'. You can refer to the .NET Form using the fully qualified name, including the namespace like this : System.Windows.Forms.Form And you can refer to your own Form like this : MyApplication.Form, where MyApplication is your namespace. So, namespaces allow you to avoid name conflicts! All classes in .NET class library are grouped into namespaces. You can use all the classes using the fully qualified name, including the namespace also along with the class name.
- System.Windows.Forms.Form
- System.String
- System.Double If you want to declare a Button object, you must do the following: System.Windows.Forms.Button myButton; It is not mandatory to use the namespace also along with the name. You may use the using directive on top of the class and safely avoid the need to write the fully qualified name every time when you refer to a class. using System.Windows.Forms.Button; if you have the above line of code on top of the class file, you don't need to type the namespace name System.Windows.Forms with all the classes in this namespace. You can simply use class name directly as shown below: Button myButton;
.NET supported languages Currently .NET supports the following languages:

- C#
- VB.NET
- C++
- J#

The above languages are from Microsoft. Many third parties are writing compilers for other languages with .NET support.