Tuesday, December 23, 2008

Basics of .Net Framework

In order to fully grasp the .NET framework, it is important to understand why the need to create the .NET framework arose, and what were the driving forces behind it.


THE WORLD BEFORE .NET: 

The answer to this goes back to the history of windows applications prior to the introduction of .NET in the year 2000. From a programming perspective, you faced a different type of annoyance based on what type of programmer you were:

 [1] C++ Programmer: You had no choice but to contend with the dirty syntactical constructs, and the ugly pointers, the C++ language threw at you. No doubt, it was an object-oriented and improvised version of C, but still had its inherited weaknesses. There was no space for cross-language integration. Your binaries could not talk with VB or Java binaries.

[2] Visual Basic 6 Programmer: In order to avoid the whole complexity and pitfalls of C++, many programmers embraced the simple approach provided by an English like language, Visual Basic. The advantage of VB6 was its ability to rapidly design Windows applications from scratch, with VB doing all the dirty work for you like object-initialization, handling the windows api, etc. You also had the option of directly calling the windows api, but this approach was not very stable. Since, the underlying windows api were written in C language, parameters could not be passed smoothly. There were some other problems too with Visual Basic. It did not have much support for object-oriented programming. The problem of cross-language compatibility still remained.

[3] Java Programmer: Java did a very good thing by cleaning up all the mess inherent in C language like pointer-arithmetic and making it object-oriented and platform-independent. The advantage was that you had a wonderful language you can code into, and run the resulting output (java bytecode) on any platform or device. But the disadvantages were twofold. First, since java did not integrate tightly with the operating system, it suffered from substandard performance, especially on Windows. Secondly, the problem of cross-language compatibility still remained. Java could not talk to any other language. Neither libraries in other languages could be used in java, nor could java bytecode be used anywhere else.

To some extent, these issues were addressed by Microsoft by providing COM (Component Object Model) . But this solution was not full-proof. COM was essentially a bunch of classes that exposed common interfaces like IDispatch and IUnknown, so that COM objects written in different languages could implement these interfaces and thus talk to each other. One limitation this approach suffered from was that object-inheritance was lacking – COM classes could not inherit from each other. This was the same limitation that VB was suffering from. Another issue was that there was very little support for web-development. COM classes were used through variant-types in a scripting language called ASP, which neither provided a truly typed language that could be compiled and also suffered from a spaghetti code of ASP mush-mashed with HTML.

THE .NET SOLUTION (Circa 2000):

Microsoft's solution to this above mess was quite simple and drastic - Change Everything!!. .NET is not just a runtime engine, or a technology that belongs to Microsoft alone. Microsoft has submitted .NET to ECMA standards, making it theoretically possible for any operating-system in the world to implement a .NET runtime engine, as per the ECMA guidelines, so that it can run a piece of code written in any .NET language on the planet.
Today, we do have some implementations of .NET even on Linux platform like the Mono and Portable.NET. Let us now see how does .NET make it possible to achieve this feat of WORA (Write Once Run Anywhere). .NET framework is basically composed of the CLR and BCL. This design is based upon the ECMA standards of CLS and CTS. These are all .NET acronyms you want to be familiar with:


.NET Framework Components:

CLR: Common Language Runtime
BCL: Base Class Library

.NET Common Language:
CIL: Common Intermediary Language


ECMA Specifications:
CTS: Common Type System
                    +
CLS: Common Language Specification
                    =
CLI: Common Language Infrastructure

Obviously, Microsoft needed a runtime execution engine that is aware of the underlying processor architecture, and is able to run any .NET compiled code. This runtime is CLR. The beauty of .NET is that the CLR is totally unaware of the Operating System or the underlying processor architecture, thus making it possible to run an assembly (CIL Code) on any .NET machine. This is conceptually similar to the JVM (Java Virtual Machine) that runs Java Bytecode instead of the Assembly. 

An additional benefit that Microsoft brought to table was Language-Agnosticism or Language-Independence. Unlike the JVM that understands and speaks only Java, the CLR is totally language-agnostic. It couldn’t care less about which language the assembly was originally compiled in! It could be written in any .NET language like VB.NET, C#, J#, F#, etc. (there are several other .NET languages too like Cobol.NET and Pascal.NET. You can know more about these implementations at www.dotnetlanguages.net). The CLS (Common Language Specification) ensures that the assembly compiled with any .NET compiler adheres to a common standard, thus making this possible.

Another important advantage of CLR is that it is equipped with a GC (Garbage Collector) that automatically does the memory management for you! A C# programmer, no longer has to contend with all the intricacies of memory allocation and ugly pointer-arithmetic that a C++ programmer suffered from. GC, in other words, is like a broom provided by CLR that keeps on cleaning up the mess created by the coder, if any.

BCL (Base Class Library): The power of any language comes from the support library that it provides to its coders. BCL is an extensive library that provides the types that are common to all .NET languages. This includes all the primitives like Threading, Collections, I/O, Graphics, Networking, etc. Namespaces are available for all of them like System.Thread, System.Collections, System.IO, System.Graphics, System.Threadng, System.Net and many more. Again, if you are from Java background, it helps that this is conceptually similar to the various packages (like java.lang) provided in Java. Also, Microsoft has its own implementation of BCL called FCL (Framework Class Library), which is a super-set of BCL. Apart from the BCL which is ECMA compliant, FCL contains classes for Microsoft's own proprietary technologies like ASP.NET and Windows Forms (System.Web and System.Windows.Forms respectively).

There is hardly any functionality that is not provided in the FCL. However, .NET languages still have the capability to use Windows API functions and talk to the OS directly, though it is seldom required. Some languages like C# also have the additional feature of writing unmanaged code i.e. code that will be run by the OS directly and not involve the CLR. Using such features, of course, will break the CLS specification and will not work on non-windows operating systems.

CLS, CTS and CLI: The real fun in .NET begins with CLS and CTS. The CTS (Common Type System) standardizes a common norm to which all .NET data types and language constructs should adhere to. However, a language may not have all features specified in the CTS, but it must implement a critical minimum of features common to all .NET languages known as CLS (Common Language Specification). Thus, a .Net class written in any language (e.g. VB.Net) that implements only those features that are in the CLS, is guaranteed to be used in any other language too (say C#). 100% language integration is thus provided. Classes written in one language can be easily used and inherited by the other language, Interfaces in one language could be implemented by another, and finally exceptions of classes in one language could be handled by another. This is called “language-agnosticism”, the ability to work with classes without caring in which language it is written in.
CLI (Common Language Infrastructure) is just a blanket term covering both CTS and CLS.

CIL (Common Intermediary Language): Formerly known as MSIL (Microsoft Intermediary Language), CIL is the common language of which assemblies are made of. The beauty of CIL is that it does not contain any CPU specific instructions. When an assembly is actually run, only then it is compiled from CIL to the actual processor-specific instructions, by a process called JIT (Just-in-time) compiling. The .NET JITer, also known as jitter, couldn't care less about the high level language the CIL was actually written in (C#, VB.net, etc).
To sum up, here is how you should visualize the .NET Framework:






AN OVERVIEW OF .NET ASSEMBLIES:

A .NET assembly is the compiled binary output of an application written in a CLS compliant language. It could be either an application executable (*.exe) or a library (*.dll). As we have already seen, a .NET assembly doesn't contain any platform-specific instruction. Instead, it contains the following things:

[1] CIL: This is the code you have written that is compiled into the CIL. Code in the form of CIL is also known as Managed code.

[2] Type Metadata: This block contains the references to other .NET types you have made in your application.

[3] Assembly Manifest: This contains the information regarding the assembly version and culture information.

The compilation process of a .NET language is as follows:





CONCLUSION:

This was just an outline of what the .NET technology is at its core, and how it came to existence. If you are a programmer and looking forward to start coding in the .NET environment, then you may start by reading through the extensive library and articles at MSDN. Apart from that, you should also begin practically enhancing your knowledge and expertise by coding in .NET languages and creating .NET programs. There are several places where help is available. Here are some of the best sites I would like to suggest:

[1] http://www.msdn.com – Microsoft Developers Network. This is the place where you can get everything first hand – complete documentation of entire framework (versions 1.1 to 4.0), code samples, articles and much more.

[2] http://www.codeproject.com – A very good site to learn .NET programming. Its a network of good developers around the world who contribute code and learn from each other.

[3] http://www.stackoverflow.com – Excellent site if you have a query in .NET and want it answered. Post here and you'll get it answered in minutes.

[4] http://www.asp.net – Good site to learn web-development in ASP.NET.

[5] http://www.codeplex.com – A project-hosting site initiated by Microsoft. You'll find several open-source projects here with their source-code available for you to browse, most of them being in .NET.

For any queries or doubts please revert back to me through the comments link on this page.

No comments:

Post a Comment