|
In
the not-too-distant past we used different programming models
for developing Windows desktop applications and Web-based
applications. Programming a Windows desktop application using
Visual Basic (VB) or Visual C++ 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 Microsofts 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 any 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 languageC# (read
as C Sharp). 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 cleaner
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.
FCL and CLR
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, serialisation,
accessing the network and the Internet, manipulating XML documents,
accessing databases, etc.
Common Language Runtime is the heart of the .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.
The adjoining table illustrates the components of the .NET
framework.
Windows
Forms, Web Forms, WebServices, etc.
(Developed in .NET compliant languages) |
.NET
Framework Base Classes
(ADO.NET, XML, Threading, I/O, Network) |
| Common
Language Runtime (Memory Management, Common Type System,
Lifecycle Monitoring) |
The
applications created in .NET compliant languages use the classes
provided by the Base Class Library. These applications run
under .NET runtime that manages the lifetime of objects created
by applications, throws up 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,
the 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. An important aspect
of the 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 the
.NET framework. The JIT compiler converts IL code into machine
code, which is then executed. The JIT compiler does not
compile the 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 on the processor type and appropriately
converts 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 one 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.
Thats it for a brief introduction to the .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.
(a) Start Microsoft Visual Studio.NET from Start | Program
| Microsoft Visual Studio.NET menu option.
(b) 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.
(c) Select Console Application from the list of
Templates.
(d) Select a location where this project should get saved.
Name the project as Simple. Click the OK button.
(e) 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.
 |
Yashavant
Kanetkar, one of the first Express Computer columnists,
is an established software expert, speaker and author
with several best-sellers to his credit, including titles
like “Let Us C” and the “Fundas” series. Contact him at
kanet@nagpur.dot.net.in |
|