Monday, August 25, 2008

The .NET Framework

Not in the distant past we used different programming models for developing Windows desktop application and web based application. Programming a Windows desktop application using VB or VC++ was pretty different than programming a web based application using technologies like ASP, COM, etc. With the launch of .NET this differentiation has been obscured. .NET offers a consistent programming model irrespective of the type of application that we intend to build. Using .NET compliant languages we can create every sort of application, be it a Windows desktop application, a web application, a database application, a DLL, a setup and deployment project, a distributed application, etc.

Many of Microsoft’s new technologies were extensions and improvements of previous ones. For example, COM was extended from OLE and COM+ was extended from COM. Obviously, this was to maintain the backward compatibility. Though these extensions could solve the problems at hand, they ultimately led to more complexities. Microsoft’s new .NET technology is a welcome deviation from this path. It has been thought out and designed from scratch. It does not mean that .NET is not backward compatible. We can import the existing components to work in .NET. We can also create components in .NET that can be used by the existing clients. .NET is a new framework for programming Windows. It provides an application execution environment that manages memory, addresses versioning issues and improves the reliability, scalability, and security of your application.

Microsoft has upgraded VB and languages like C++, COBOL, etc. to target the .NET environment. In addition to these languages, it has provided a fresh .NET compliant language—C#. It is natural to think that migration from Microsoft’s popular Visual Basic language to VB.NET or transition from VC++ to VC++.NET would be smooth. But one quick look at C# would convince you that in many cases coding in C# is more clean, pleasant and efficient.

There are two main components of .NET framework—.NET Framework Base Class Library (FCL) and Common Language Runtime (CLR). Let us understand each of them.

The base class library is an extensive common class library that can be used by any .NET compliant language. These classes encapsulate a high degree of functionality that programmers can use in their programs. The library includes classes for creating and displaying windows forms, web forms, serialization, accessing network and internet, manipulating XML documents, accessing databases, etc.

Common Language Runtime is the heart of .NET framework. It actually manages the code during execution. The code that runs under CLR is called ‘managed code’. The code that is executed under .NET runtime gets benefits like cross-language inheritance, cross-language exception handling, enhanced security, versioning and deployment support, a simplified model for component interaction, and debugging and profiling services.

Following figure illustrates the components of .NET framework.

The applications created in .NET compliant languages use the classes provided by base class library. These applications run under .NET runtime that manages the lifetime of objects created by applications, throws exceptions, etc.
Let us now take a look at other important features of .NET.

Common Type System: Language interoperability under .NET is possible only when all the languages share a common data type system. For this, common type system (CTS) is introduced. CTS ensures that an int in C# is same as an int in VC++. Under CTS, all the classes are derived from the System.Object class and all the primitive data types are mapped to the structures defined in base class library. CTS also specifies the visibility level (from where the type should be accessible) of data types.

Intermediate Language: A .NET programming language does not compile into executable code; instead it compiles into an intermediate code called Microsoft Intermediate Language (MSIL). IL is a CPU-independent language. The IL code is sent to the CLR that converts the code to machine language using the Just In Time compiler, which is then run on the host machine. Important aspect of IL language is that it provides the hardware abstraction layer. We can view the IL code of our application using the ILDASM tool shipped with Visual Studio.NET.

JIT Compilation: JIT (Just In Time) compiler is a crucial component of .NET framework. The JIT compiler converts IL code into machine code, which is then executed. The JIT compiler does not compile entire code at once because it could hamper the performance of the program. It compiles the code at runtime, at the time it is called. The code that is compiled gets stored until the execution comes to an end. This avoids recompilation of code. The reason why conversion from IL code to machine code takes place at runtime is that, the JIT first gets information of the processor type and convert the IL code so that it would run on that type of processor.

Assemblies: An assembly is a unit containing IL code of a program. It is similar to a DLL file, but difference is that unlike DLL, an assembly is self-describing. Assemblies contain assembly metadata (or manifest) that gives details of the assembly, type metadata describing the types, methods, etc. defined in the assembly and resources.

Garbage Collection: Garbage Collection is a program that is invoked by the CLR to free the memory that is not being used by the application. Because of this technique the programmers no more need to take care of memory leakages, dangling pointers and clean up of memory.
I hope that what we saw above would give you a glimpse of .NET framework. Let us now create a simple program in C# that uses the .NET framework. It is assumed that you have installed Visual Studio.NET on a machine running under Windows NT/2000/XP.

The First C# Program
The steps to create the program are given below.

  • Start Microsoft Visual Studio.NET from ‘Start | Program | Microsoft Visual Studio.NET’ menu option.

  • Create a new C# project by selecting ‘File | New | Project’ menu option. A ‘New Project’ dialog would appear. From the ‘New Project’ dialog box select project type as ‘Visual C# Projects’.

  • Select ‘Console Application’ from the list of Templates.

  • Select a location where this project should get saved. Give name to the project as Simple. Click the OK button.

  • A ‘Class1.cs’ file (cs stands for CSharp) would get created with the skeleton code given below.

using System ;
namespace Simple
{

class Class1
{

static void Main ( string[ ] args )
{

}

}

}

Now, add the following statement that would display the string “Hello world” on screen.

Console.WriteLine ( “Hello C#” ) ;

Compile and execute the program by pressing Ctrl + F5. The program execution starts from the Main( ) function. Since C# is a pure object-oriented language it does not allow us to create global variables and functions. Instead, all variables and functions including Main( ) should be defined inside a class.

To send output to the screen we have used the WriteLine( ) function. We can display any data type through this function. Since WriteLine( ) belongs to the Console class we have to use the form Console.WriteLine( ) to call it.

To be able to use the Console class in any program we need to import it from a namespace, which has been done in our program through the statement using System. Like the Console class our class is also enclosed in the Simple namespace.

Those who have not done C++ programming may find the terms class and namespace confusing. We would discuss them in detail in the forthcoming articles.

No comments:

Your Title