Introduction
Being a contributor to the Code Project for quite some time now, it is commendable to see so many articles from various folks in the industry talk about the features of .NET and how a specific features work or what are some of the tips and tricks of the trade. This fever of .NET is very interesting to watch and rest assured that the storms caused by .NET will be as great as the storms caused by C++ when it was introduced. Walking down the web-site, I saw lots of articles on .NET, but I did not see one on: What is NET? What is it made up of? Why is there so much interest in it?
This article is a dedication to the above answers. In this article, I will give an understanding of what is .NET and why it came into existence. We will also see some of the core building blocks of .NET and how it is layered. For a deeper insight into each of the building blocks, you anyways have lots of good articles on the Code Project web site. So happy reading!
Why .NET
The world of computing till date has been chaotic. We have had various languages struggling to interoperate with each other, developers undergoing huge learning curves to shift from one language to another or from one application type to another, non-standard ways of modeling applications and designing solutions and huge syntactic differences between languages. The list goes on....
Past years have seen some solace in the form of enterprise "glue" applications and standards like COM, which put-forth a binary standard of interoperability between application components. But in reality, this was not always true (VB COM found it very difficult to take on VC++ COM). Also, as applications increased in their reach, it was found that rather than re-inventing the wheel for a solution, it was better to take the "service" of another applications specialized for a piece of work.
Thus from a paradigm where applications replicated code to provide common services, we have moved to a paradigm where applications are built as "collaborative units" of components working together. This simple shift has led to the collapse of the current set of architectures and demanded a new programming model:
- A model where applications can be built as reusable components and are sharable over the internet.
- A model that encourages applications to be shared as a "service" (read web services).
- A model that enables true "interoperability" wherein the language used is only a matter of choice, thus enabling organizations to take advantage of existing skill sets.
Enter .NET. The .NET Framework is a new computing platform developed by Microsoft that simplifies application development in the highly distributed environment of the internet. .NET is much more than just a platform for developing for the internet, but it is intended for this purpose predominantly, because here, others methods have failed in the past.
Overview of .NET
The .NET Framework has been developed to cater to the following objectives and requirements:
- To provide a consistent object-oriented environment to develop applications.
- To provide a code execution environment that simplifies deployment and versioning.
- To provide a code execution environment that guarantees the safety of the code that is executing. This includes both code developed internally by an organization or for code developed by 3rd party vendors.
- To provide a code execution environment that eliminates the issues faced by scripted environments with respect to performance.
- To provide a common programming model where the choice of a programming language becomes a matter of choice.
The .NET Framework is made up of two major components: the common language runtime (CLR) and the framework class library (FCL). The CLR is the foundation of the .NET Framework and provides various services that applications can use. The CLR also forms the �environment� that other applications run on. The FCL is a collection of over 7000+ types that cater to all the services, and data structures that applications will ever need.
The following diagram shows the .NET Framework, its hierarchy and the associated toolset. The diagram is so famous that you can spend some time memorizing its layout!!
At the base of the diagram, you see the operating system which can be (theoretically) any platform. The Common Language Runtime (CLR) is the substrate that abstracts the underlying operating system from your code. The minute it does this, it means that your code has to run using the services provided by the CLR and we get a new name called managed code. The CLR provides its services to applications by providing a standard set of library classes that abstract all the tasks that you will ever need. These classes are called as the Base Class Libraries. On top of this, other development platforms and applications are built (like ASP.NET, ADO.NET and so on). Language compilers that need to generate code for the CLR must adhere to a common set of specifications as laid down by the Common Language Specification (CLS). Above this, you have all the popular .NET languages.
Visual Studio .NET, then is the "glue" that helps your generate .NET applications and provides an IDE that is excellent for collaborative development.
In the subsequent sections, we will delve into the core layers of the .NET framework. Note that application development layers (like ADO.NET, ASP.NET etc) and development tools (VS.NET) are not dealt with.
Common Language Runtime
The CLR is the platform on which applications are hosted and executed. The CLR also provides a set of services that applications can use to access various resources (like arrays, collections, operating system folders etc). Since this runtime "manages" the execution of your code, code that works on the CLR is called as managed code. Any other code, you guessed it, is called unmanaged code.
Compilers and tools expose the CLR's functionality and enable you to write code that benefits from this managed execution environment. To enable the runtime to provide services to managed code, language compilers must also emit metadata that describes the types that we develop in .NET. This metadata is stored along with the type file and makes it "self-describing". Using this information, the runtime automatically handles object layout and manages references to objects, releasing them when they are no longer being used.
When compilers emit code to run on the CLR, they do not emit machine language code. Rather, an intermediate language code is used called Microsoft Intermediate Language (MSIL). MSIL is like an object-oriented version of assembly language and is platform independent. It has a rich set of instructions that enable efficient representation of the code. When a code starts to execute, a process knowing as Just in Time Compilation (JIT) converts the MSIL code into the native processor instructions of the platform, which is then executed. This is shown in the following diagram:
Note that this conversion happens only once. Subsequent calls to the code will execute the native version only. Once the application dies down and is started again, this process is repeated.
The following are some of the benefits of the CLR:
- Performance improvements.
- The ability to easily use components developed in other languages.
- Extensible types provided by a class library.
- New language features such as inheritance, interfaces, and overloading for object-oriented programming; support for explicit free threading that allows creation of multithreaded, scalable applications; support for structured exception handling and custom attributes.
Common Language Specification
Language interoperability is the ability of code to interact with code that is written using a different programming language. Language interoperability can help maximize code reuse and, therefore, improve the efficiency of the development process. Because developers use a wide variety of tools and technologies, each of which might support different features and types, it has historically been difficult to ensure language interoperability. However, language compilers and tools that target the common language runtime benefit from the runtime's built-in support for language interoperability. To ensure that you can develop managed code that can be fully used by developers using any programming language, a set of language features and rules for using them called the Common Language Specification (CLS) has been defined. Components that follow these rules and expose only CLS features are considered CLS-compliant.
To fully interact with other objects regardless of the language they were implemented in, objects must expose to callers only those features that are common to all the languages they must interoperate with. If your component uses only CLS features in the API that it exposes to other code (including derived classes), the component is guaranteed to be accessible from any programming language that supports the CLS. Components that adhere to the CLS rules and use only the features included in the CLS are said to be CLS-compliant components.
Common Type System
The common type system defines how types are declared, used, and managed in the runtime, and is also an important part of the runtime's support for cross-language integration. The common type system performs the following functions:
- Establishes a framework that enables cross-language integration, type safety, and high performance code execution.
- Provides an object-oriented model that supports the complete implementation of many programming languages.
- Defines rules that languages must follow, which helps ensure that objects written in different languages can interact with each other. For example, if you have created a class in VB.NET, you can inherit from it in a C# program.
Framework Class Library
Windows programmers coding in C, tend to rely on the Windows API and functions in third-party DLLs to get their job done. C++ programmers often use class libraries of their own creation or standard class libraries such as MFC. Visual Basic programmers use the Visual Basic API, which is an abstraction of the underlying operating system API.
In the .NET Framework, all these anachronistic API�s are done away with. Rather a new set of functions branded as the framework class library are introduced which contain more than 7000 types.
To make learning and using the FCL more manageable, Microsoft has divided the FCL into hierarchical namespaces. The FCL has about 100 namespaces in all. Each namespace holds classes and other types that share a common purpose. For example, much of the window manager portion of the Windows API is encapsulated in the System.Windows.Forms namespace. In this namespace classes that represent windows, dialog boxes, menus, and other elements commonly used in GUI applications are present. A separate namespace called System.Collections holds classes representing hash tables, resizable arrays, and other data containers. Yet another namespace, System.IO, contains classes for doing file I/O.
The following diagram shows the FCL classes and their associated namespaces.
What's Next
Hopefully, this article has distilled some of the terms in the .NET platform and explained why .NET is required. The .NET framework is a huge ocean and it will take some time for applications to be mature in it. Microsoft is also gearing to release its next version of server operating systems (2003) which provide lots of features for .NET applications. Expect the next version of SQL Server (code named Yukon) to have a .NET flavor too!! It is important, thus, to understand what the framework provides to us and what new features can applications target in the future and this is where this article will help.