You are on page 1of 17

UNIT I

The Philosophy of .NET


.NET is a collection of tools, technologies, and languages that all work together in a framework
to provide the solutions that are needed to easily build and deploy truly robust enterprise applications.

The first thing that you should notice when looking at this diagram is that the .NET Framework sits on
top of the operating system. Presently, the operating systems that can take the .NET Framework
include Windows XP, Windows 2000, and Windows NT.

NOTE: The support for the .NET Framework on Windows NT is limited to functioning only as a
client. Windows NT will not support the Framework as a server.

• At the base of the .NET Framework is the Common Language Runtime (CLR). The CLR is the
engine that manages the execution of the code.
• The next layer up is the .NET Framework Base Classes. This layer contains classes, value
types, and interfaces that you will use often in your development process. Most notably within
the .NET Framework Base Classes is ADO.NET, which provides access to and management of
data.
• The third layer of the framework is ASP.NET and Windows Forms. ASP.NET should not be
viewed as the next version of Active Server Pages after ASP 3.0, but as a dramatically new
shift in Web application development. Using ASP.NET, it’s now possible to build robust Web
applications that are even more functional than Win32 applications of the past. This was
always quite difficult to do in the stateless nature of the Internet, but ASP.NET offers a number
of different solutions to overcome the traditional limitations on the types of applications that
were possible. The ASP.NET section of the .NET Framework is also where the XML Web
services model resides.
• The second part of the top layer of the .NET Framework is the Windows Forms section. This is
where you can build the traditional executable applications that you built with Visual Basic 6.0
in the past. There are some new features here as well, such as a new drawing class and the
capability to program these applications in any of the available .NET languages. You may view
XML Web services as something you use within the ASP.NET applications that you build, but
this isn’t the only place where you can consume XML Web services. It's quite possible to
consume XML Web services within a Windows Form application and a number of other
application types, just as you can from any ASP.NET Web application.

Mrs. Reena Vimal A. G.


Assistant Professor, MCA Dept.
TOCE
The .NET Framework
To get around the problem of having multiple development environments, Microsoft developed
an environment that is a unified development environment. This framework is the platform for the
entire .NET idea. The framework is language-neutral and built to provide you with the tools and
solutions that you need to build rich applications in the stateless Environment of the Internet. The
.NET Framework includes two main components: the Common Language Runtime and the Base Class
Libraries.

The .NET languages


In the past, you chose the development language for an application based upon the
functionality that you were trying to perform. Some languages were more powerful than others, but at
the same time they might have required a higher level of understanding and were, in most cases, more
difficult to program in. Now the .NET Framework provides you with a language-independent
programming platform. In .NET, no one language is superior to any of the other languages. They all
have equal access to everything that .NET offers. To be part of the .NET Framework, a language only
has to follow certain rules. The biggest and most important rule for inclusion is that the language
needs to be an object-oriented language. Microsoft provides four languages with the .NET Framework:
Visual Basic .NET, C#, C++.NET, and JScript .NET. Microsoft also provides J# (pronounced J-
sharp), but in order to use this new language that is basically Java for .NET

C#
C# (pronounced "C sharp") is a simple, modern, object oriented, and type-safe programming language
derived from C and C++. C# is built specifically to program the MICROSOFT.NET framework. The
.NET framework consists of a run-time environment known as the Common Language Runtime
(CLR), and a set of class libraries, which provide a rich development platform that can be exploited by
a variety of languages and tools. C# aims to combine the high productivity of Visual Basic and the raw
power of C++. C# is provided as part of Microsoft Visual Studio 7.0. In addition to C#, Visual Studio
supports Visual Basic, Visual C++, and the scripting languages VBScript and JScript. All of these
languages provide access to the Microsoft .NET platform, which includes a common execution engine
and a rich class library. For C# developers, even though C# is a new language, it has complete access
to the same rich class libraries that are used by seasoned tools such as Visual Basic and Visual C++.
C# itself does not include a class library. It's built with the Web in mind and claims to provide an
environment that's in sync with HTML, XML, and SOAP.

Mrs. Reena Vimal A. G.


Assistant Professor, MCA Dept.
TOCE
The .NET Solution

The .NET Framework is a completely new model for building systems on the Windows family of
operating systems, as well as on numerous non-Microsoft operating systems such as Mac OS X and
various Unix/Linux distributions. Some of the core features provided courtesy of .NET:

• Comprehensive interoperability with existing code: Existing COM binaries can commingle
(i.e., interop) with newer .NET binaries and vice versa. Also, PlatformInvocation Services
(PInvoke) allows calling C-based functions from managed code.
• Complete and total language integration: .NET supports cross-language inheritance, cross-
language exception handling, and cross-language debugging of code.
• A common runtime engine shared by all .NET-aware languages: One aspect of this engine is
a well-defined set of types that each .NET-aware language “understands.”
• A comprehensive base class library: This library provides shelter from the complexities of
raw API calls and offers a consistent object model used by all .NET-aware languages.
• No more COM plumbing: IClassFactory, IUnknown, IDispatch, IDL code, and the evil
variant compliant data types (BSTR, SAFEARRAY, and so forth) have no place in a .NET
binary.
• A truly simplified deployment model: Under .NET, there is no need to register a binary unit
into the system registry. Furthermore, .NET allows multiple versions of the same *.dll to exist
in harmony on a single machine.

Building Blocks of .Net Platform (CLR, CTS, CLS)

There are three building blocks of .NET Framework, which are as follows:
• Common Language Runtime (CLR): It is used to execute the managed code. It was designed
to support multiple languages.
• Common Type System (CTS): It provides every language running on the .NET platform with a
base set of data types.
• Common Language Specification (CLS): It describes a common level of language
functionality. It is a set of rules that a language compiler must have in order to create .NET
applications that run in the CLR.

CLR (Common Language Runtime): Programmatically speaking, the term runtime can be
understood as a collection of external services that are required to execute a given compiled unit of
code. The CLR is the layer of the .NET Framework that makes the source code into an intermediate
language which is language independent. This language is called the Microsoft Intermediate
Language.

The key difference between the .NET runtime and the various other runtimes is the fact that the .NET
runtime provides a single well-defined runtime layer that is shared by all languages and platforms that
are .NET-know.

The primary role of the CLR is to locate, load, and manage .NET types on your behalf. The CLR also
takes care of a number of low-level details such as memory management, language integration and
performing security checks.

Mrs. Reena Vimal A. G.


Assistant Professor, MCA Dept.
TOCE
CTS (Common Type System): The CTS specification fully describes all possible data types and
programming constructs supported by the runtime, specifies how these entities can interact with each
other, and details how they are represented in the .NET metadata format

Every language running on the .NET platform has a base set of data types provided by CTS. CTS is
responsible for defining type of .NET languages. Most of the languages running on .NET platform use
aliases for using those CTS types. For example, a four-byte integer value is represented by the CTS
type System.Int32. C# defines an alias for this called type called int and in VB.NET called Integer.

CLS (Common Language Specification): CLS is a related specification that defines a subset of
common types and programming constructs that all .NET programming languages can agree on. The
CLS represents the guidelines defined by for the .NET Framework. These specifications are normally
used by the compiler developers and are available for all languages, which target the .NET
Framework.

The Role of .NET Base Class Libraries

• The Base Class Libraries (BCL) are a set of classes, value types, and interfaces that give us
access to specific developer utilities and various system functions.
• All the languages that sit on top of the .NET Framework have equal access to all the classes
that are contained within the BCL.
• We use the SQLConnection class in Visual Basic .NET to connect to SQL Server exactly the
way you do in C#, although the language semantics are different.
• This table is not a comprehensive list of available namespaces.
• We can find all the namespaces within the .NET SDK documentation. We can create our own
namespaces to use within our applications.

Table 1-1 gives a brief description of some of the classes available with the .NET Framework.
Namespace Description
System Provides base data types and almost 100 classes that
deal with situations like exception handling,
mathematical functions, and garbage collection.
System.CodeDom Provides the classes needed to produce source files in
all the .NET languages.

Mrs. Reena Vimal A. G.


Assistant Professor, MCA Dept.
TOCE
System.Collections Provides access to collection classes such as lists,
queues, bit arrays, hash tables, and dictionaries.
System.ComponentModel Provides classes that are used to implement runtime
and design-time behaviors of components and controls.
System.Configuration Provides classes and interfaces that allow you to
programmatically access the various configuration files
that are on your system, such as the web.config and the
machine.config files.
System.Data Provides classes that allow data access and
manipulation to SQL Server and OleDb data sources.
These classes make up the ADO.NET architecture.
System.Diagnostics Provides classes that allow you to debug and trace your
application. There are classes to interact with event
logs, performance counters, and system processes.
Provides classes that allow you to access Active
System.DirectoryService s
Directory.
System.Drawing Provides classes that allow you to access the basic and
advanced features of the new GDI+ graphics
functionality.
System.EnterpriseServic Provides classes that allow you to access COM+
es services.
System.Globalization Provides classes that access the global system
variables, such as calendar display, date and time
settings, and currency display settings.
System.IO Provides classes that allow access to file and stream
control and manipulation.
System.Management Provides access to a collection of management
information and events about the system, devices, and
applications designed for the Windows Management
Instrumentation (WMI) infrastructure.
System.Messaging Provides classes that allow you to access message
queue controls and manipulators.
System.Net Provides access to classes that control network
services. These classes also allow control over the
system s sockets.
System.Reflection Provides classes that allow control to create and invoke
loaded types, methods, and fields.

System.Resources
Provides classes that allow you to create and lt ifi

What C# brings to the Table


C# is a hybrid of numerous languages, and is a product that is as syntactically clean than Java, is about
as simple as VB6, and provides just about as much power and flexibility as C++. C# offers the
following features:
• No pointers required! C# programs typically have no need for direct pointer manipulation.
• Automatic memory management through garbage collection: C# does not support a delete
keyword.
• Formal syntactic constructs for classes, interfaces, structures, enumerations, and delegates.
• The C++-like ability to overload operators for a custom type, without the complexity.
• Full support for interface-based programming techniques.
• Full support for aspect-based programming techniques via attributes.

Mrs. Reena Vimal A. G.


Assistant Professor, MCA Dept.
TOCE
The Role of Common Intermediate Language
• CIL (Common Intermediate Language) is also called IL or MSIL (MicroSoft Intermediate
Language).
• The source code from high-level language (C#, Basic or other language) is compiled in CIL
and stored into an assembly.
• The assembly is stored in a file in the PE (Portable Executable) format, which is also that of
.dll and .exe, and includes a manifest file containing the metadata of the assembly, which is the
interface of the code with other components of the software that uses it.

Benefits of CIL:

1. Language integration: Since each .NET –aware language produces the same CIL, all languages
are able to interact within the same binary arena.
2. As CIL is platform agnostic, the .NET becomes a platform independent architecture.

The Role of .NET type Metadata


• In addition to CIL instructions, a .NET assembly contains full, complete, and accurate
metadata, which describes each and every type (class, structure, enumeration, and so forth)
defined in the binary, as well as the members of each type (properties, methods, events, and so
on).
• It is always the job of the compiler to emit the latest and greatest type metadata. Because .NET
metadata is so wickedly meticulous, assemblies are completely self-describing entities.
• To illustrate the format of .NET type metadata, let’s take a look at the metadata that has been
generated for the Add() method of the C# Calc class

TypeDef #2 (02000003)
-------------------------------------------------------
TypDefName: CalculatorExample.Calc (02000003)
Flags : [NotPublic] [AutoLayout] [Class]
[AnsiClass] [BeforeFieldInit] (00100001)
Extends : 01000001 [TypeRef] System.Object
Method #1 (06000003)
-------------------------------------------------------
MethodName: Add (06000003)
Flags : [Public] [HideBySig] [ReuseSlot] (00000086)
RVA : 0x00002090
ImplFlags : [IL] [Managed] (00000000)
CallCnvntn: [DEFAULT]
hasThis
ReturnType: I4
2 Arguments
Argument #1: I4
Argument #2: I4
2 Parameters
(1) ParamToken : (08000001) Name : x flags: [none] (00000000)
(2) ParamToken : (08000002) Name : y flags: [none] (00000000)

Mrs. Reena Vimal A. G.


Assistant Professor, MCA Dept.
TOCE
The Role of the Assembly Manifest
• A .NET assembly also contains metadata that describes the assembly itself (technically termed
a manifest).
• Among other details, the manifest documents all external assemblies required by the current
assembly to function correctly, the assembly’s version number, copyright information, and so
forth.
• Like type metadata, it is always the job of the compiler to generate the assembly’s manifest.
• In a nutshell, this manifest documents the list of external as well as various characteristics of
the assembly itself (version number, module name, and so on).

Compiling CIL to Platform-Specific Instructions


• Due to the fact that assemblies contain CIL instructions, rather than platform-specific
instructions, CIL code must be compiled on the fly before use.
• The entity that compiles CIL code into meaningful CPU instructions is termed a just-in-time
(JIT) compiler, which sometimes goes by the friendly name of Jitter.
• The .NET runtime environment leverages a JIT compiler for each CPU targeting the runtime,
each optimized for the underlying platform.
• Developers can write a single body of code that can be efficiently JIT-compiled and executed
on machines with different architectures.
• Furthermore, as a given Jitter compiles CIL instructions into corresponding machine code, it
will cache the results in memory in a manner suited to the target operating system.
• In this way, if a call is made to a method named PrintDocument(), the CIL instructions are
compiled into platform specific instructions on the first invocation and retained in memory for
later use. Therefore, the next time PrintDocument() is called, there is no need to recompile the
CIL.

Understanding the 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 helps enable 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.
• Provides a library that contains the primitive data types (such as Boolean, Byte, Char, Int32,
and UInt64) used in application development.

Common Type System (CTS) describes a set of types that can be used in different .Net languages in
common. These types can be Value Types or Reference Types. The Value Types are passed by values
and stored in the stack. The Reference Types are passed by references and stored in the heap. Common
Type System (CTS) provides base set of Data Types which is responsible for cross language
integration. The Common Language Runtime (CLR) can load and execute the source code written in
any .Net language, only if the type is described in the Common Type System (CTS).

Mrs. Reena Vimal A. G.


Assistant Professor, MCA Dept.
TOCE
Diagram representing Common Type System and categories

• A given assembly may contain any number of distinct types. In the world of .NET, type is
simply a general term used to refer to a member from the set {class, interface, structure,
enumeration, delegate}.
• It is important, however, for all .NET programmers to learn about how to work with the five
types defined by the CTS in their language of choice.

CTS Class Types


• Every .NET-aware language supports, at the very least, the notion of a class type, which is the
cornerstone of object-oriented programming (OOP).
• A class may be composed of any number of members (such as properties, methods, and events)
and data points (fields).
• In C#, classes are declared using the class keyword:

// A C# class type.
class Calc
{
public int Add(int x, int y)
{ return x + y; }
}
class program
{
static void Main(string[] args)
{
Calc cal = new Calc();
int ans = cal.Add(5,4);
Console.WriteLine(“The result is {0}”,ans);
}
}
• CTS allows a given class to support virtual and abstract members that define a polymorphic
interface for derived classes.
• CTS classes may only derive from a single base class.
• Multiple inheritance is not allowed for class types.

Mrs. Reena Vimal A. G.


Assistant Professor, MCA Dept.
TOCE
• Table 1-1 documents a number of characteristics pertaining to class types.

Table 1-1. CTS Class Characteristics


Class Characteristic Meaning in Life
Is the class “sealed” or not? Sealed classes cannot function as a base class to other
classes.
Does the class implement any interfaces? An interface is a collection of abstract members that provide
a contract between the object and object user. The CTS
allows a class or structure to implement any number of
interfaces.
Is the class abstract or concrete? Abstract classes cannot be directly created, but are intended
to define common behaviors for derived
types. Concrete classes can be created directly.
What is the “visibility” of this class? Each class must be configured with a visibility attribute.
Basically, this trait defines whether the class may be used by
external assemblies or only from within the defining
assembly.

CTS Interface Types


• Interfaces are nothing more than a named collection of abstract member definitions, which
may be supported (i.e., implemented) by a given class or structure.
• Interfaces are the only .NET type that do not derive from a common base type.
• In C#, interface types are defined using the interface keyword, for example:
// A C# interface type is usually declared as public, to allow types in other
// assemblies to implement their behavior.
using System;

public interface Interface_demo


{
void Show();
}

class Interface_imp:Interface_demo
{
public void Show()
{
Console.WriteLine("Show() method Implemented");
}

public static void Main(string[] args)


{
Interface_imp inter = new Interface_imp();
inter.Show();
}
}
• Interfaces are of little use.
• However, when a class or structure implements a given interface in its unique way, you are
able to request access to the supplied functionality using an interface reference in a
polymorphic manner.

CTS Structure Types


• A structure can be thought of as a lightweight class type having value-based semantics.
• CTS structures may define any number of parameterized constructors.

Mrs. Reena Vimal A. G.


Assistant Professor, MCA Dept.
TOCE
• Typically, structures are best suited for modeling geometric and mathematical data, and are
created in C# using the struct keyword:
// A C# structure type.
struct Point
{
// Structures can contain fields.
public int xPos, yPos;
// Structures can contain parameterized constructors.
public Point(int x, int y)
{
xPos = x; yPos = y;
}
// Structures may define methods.
public void Display()
{
Console.WriteLine("({0}, {1}", xPos, yPos);
}
}
class Program
{
static void Main(string[] args)
{
Point p = new Point(3,4);
p.Display();
}
}

CTS Enumeration Types


• Enumerations are handy programming constructs that allow you to group name/value pairs.
• Rather than keeping track of raw numerical values to represent each possibility, we can build a
custom enumeration using the enum keyword:
// A C# enumeration type.

class enumerations
{
public enum Directions
{
North=0,
South,
East,
West
}
public static void Main()
{
Console.WriteLine(“North = {0}”,Directions.North);
Console.WriteLine(“South = {0}”,Directions.South);

}
}

Mrs. Reena Vimal A. G.


Assistant Professor, MCA Dept.
TOCE
• By default, the storage used to hold each item is a 32-bit integer(System.Int32).
• By default, the first enumerator has the value zero, and the value of each successor is
incremented by 1.
• The CTS demands that enumerated types derive from a common base class, System.Enum.

CTS Delegate Types


• Delegates are the .NET equivalent of a type-safe C-style function pointer.
• The key difference is that a .NET delegate is a class that derives from
System.MulticastDelegate, rather than a simple pointer to a raw memory address.
• In C#, delegates are declared using the delegate keyword:
namespace MyFirstDelegate
{
// This C# delegate type can "point to" any method
// returning an integer and taking two integers as input.
delegate int MyDele(int x, int y);
public class MyClass
{
void add(int x,int y)
{
return x+y;
}
}
public class program
{
public static void Main()
{
MyDele del = new MyDele(MyClass.add);
int addResult = del(5,5);
Console.WriteLine(“Addition ={0}”,addResult);
}
}
}

• Delegates are useful when we wish to provide a way for one entity to forward a call to another
entity.
• Delegates provide intrinsic support for multicasting and synchronous method invocations.

CTS Type Members


• Most types take any number of members.
• Formally speaking, a type member is constrained by the set {constructor, static constructor,
nested type, operator, method, property, field, constant, event}.
• Each member has a given visibility trait (e.g., public, private, protected, and so forth).
• Some members may be declared as abstract to enforce a polymorphic behavior on derived
types as well as virtual to define a canned (but overridable) implementation.
• Also, most members may be configured as static (bound at the class level) or instance (bound
at the object level).

Mrs. Reena Vimal A. G.


Assistant Professor, MCA Dept.
TOCE
Intrinsic CTS Data Types
• CTS establish a well-defined set of fundamental data types.
• Although a given language typically has a unique keyword used to declare an intrinsic CTS
data type, all language keywords ultimately resolve to the same type defined in an assembly
named mscorlib.dll.
• Consider Table 1-2, which documents how key CTS data types are expressed in various .NET
languages.

Table 1-2. The Intrinsic CTS Data Types


CTS Data VB .NET C#
C++/CLI Keyword
Type Keyword Keyword
System.Byte Byte byte unsigned char
System.SByte SByte sbyte signed char
System.Int16 Short short short
System.Int32 Integer int int or long
System.Int64 Long long __int64
System.UInt16 UShort ushort unsigned short
System.UInt32 UInteger uint unsigned int or unsigned long
System.UInt64 ULong ulong unsigned __int64
System.Single Single float Float
System.Double Double double Double
System.Object Object object Object^
System.Char Char char wchar_t
System.String String string String^
System.Decimal Decimal decimal Decimal
System.Boolean Boolean bool Bool

• Consider the following code snippets, which define 32-bit numerical variables in C# and VB
.NET, using language keywords as well as the formal CTS type:
// Define some "ints" in C#.
int i = 0;
System.Int32 j = 0;
' Define some "ints" in VB .NET.
Dim i As Integer = 0
Dim j As System.Int32 = 0

Mrs. Reena Vimal A. G.


Assistant Professor, MCA Dept.
TOCE
Understanding the Common Language Specification
• The Common Language Specification (CLS), which is a set of basic language features needed
by many .Net applications to fully interact with other objects regardless of the language in
which they were implemented.
• The CLS represents the guidelines defined by for the .NET Framework.
• A CLS is a set of guidelines that describe in detail, the minimal and complete set of features a
given .NET aware compiler must support to produce code that can be hosted by the CLR,
while at the same time be accessed in a uniform manner by all languages that target the .NET
platform.
• CLS can be viewed as a physical subset of the full functionality defined by CTS.
• CLS is ultimately a set of rules that compiler builders must conform to, if they intend their
products to function within the .NET universe.
• Each rule is assigned a simple name and describes how this rule affects those who build the
compiler as well as those who interact with them.
• For example,
RULE 1: CLS rules apply only to those parts of a type that are exposed outside the
defining assembly.
• In addition to Rule 1, the CLS defines numerous other rules.
• For example, CLS describes how a given language must represent text strings, how
enumerations should be represented internally, how to use static types, and so forth.
• C# compiler will check the code for CLS compliance.
• If any non-CLS-compliant syntactic tokens are discovered, a complier error will be generated.

Understanding the Common Language Runtime

• 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 term runtime can be understood as a collection of external services that are required to
execute a given compiled unit of code.
• The key difference between the .NET runtime and the various other runtimes is the fact that the
.NET runtime provides a single well-defined runtime layer that is shared by all languages and
platforms that are .NET aware.
• The CLR is physically represented by a library named mscoree.dll (the Common Object
Runtime Execution Engine).
• When an assembly is referenced for use, mscoree.dll is loaded automatically, which in turn
loads the required assembly into memory.
• The runtime engine is responsible for a number of tasks. First and foremost, it is the entity in
charge of resolving the location of an assembly and finding the requested type within the
binary by reading the contained metadata.
• The CLR then lays out the type in memory, compiles the associated CIL into platform specific
instructions, performs any necessary security checks, and then executes the code in question.
• In addition to loading our custom assemblies and creating our custom types, the CLR will also
interact with the types contained within the .NET base class libraries when required.
• Although the entire base class library has been broken into a number of discrete assemblies, the
key assembly is mscorlib.dll.

Mrs. Reena Vimal A. G.


Assistant Professor, MCA Dept.
TOCE
• mscorlib.dll contains a large number of core types that encapsulate a wide variety of common
programming tasks as well as the core data types used by all .NET languages.
• When we build .NET solutions, we automatically have access to this particular assembly.
• Figure illustrates the workflow that takes place between source code (which is making use of
base class library types), a given .NET compiler, and the .NET execution engine.

• 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 multi
language support, code security, object lifetime management, and memory management.

Mrs. Reena Vimal A. G.


Assistant Professor, MCA Dept.
TOCE
A tour of the .NET Namespaces
• A namespace is a grouping of semantically related types contained in an assembly.
• For example, the System.IO namespace contains file I/O–related types, the System.Data
namespace defines basic database types, and so on.
• It is very important to point out that a single assembly (such as mscorlib.dll) can contain any
number of namespaces, each of which can contain any number of types.
• Clearly, our primary goal as a .NET developer is to get to know the wealth of types defined in
the (numerous) .NET namespaces.
• The most fundamental namespace to get your hands around is named System.

Table 1-3. A Sampling of .NET Namespaces


.NET Namespace Meaning in Life
System Within System, you find numerous useful types dealing with
intrinsic data, mathematical computations, random number
generation, environment variables, and garbage collection, as well as
a number of commonly used exceptions and attributes.

System.Collections These namespaces define a number of stock container types,


System.Collections.Generic as well as base types and interfaces that allow you to build
customized collections.
System.Data These namespaces are used for interacting with relational
System.Data.Odbc databases using ADO.NET.
System.Data.OracleClient
System.Data.OleDb
System.Data.SqlClient
System.IO These namespaces define numerous types used to work with
System.IO.Compression file I/O, compression of data, and port manipulation.
System.IO.Ports
System.Reflection These namespaces define types that support runtime type
System.Reflection.Emit discovery as well as dynamic creation of types.
System.Runtime.InteropServices This namespace provides facilities to allow .NET types to
interact with “unmanaged code” (e.g., C-based DLLs and COM
servers) and vice versa.
System.Drawing These namespaces define types used to build desktop
System.Windows.Forms applications using .NET’s original UI toolkit (Windows
System.Windows The System.Windows namespace is the root for several
System.Windows.Controls namespaces that represent the Windows Presentation
System.Windows.Shapes Foundation (WPF) UI toolkit.
System.Linq These namespaces define types used when programming
System.Xml.Linq against the LINQ API.
System.Data.Linq
System.Web This is one of many namespaces that allow you to build
ASP.NET web applications.
System.ServiceModel This is one of many namespaces used to build distributed
applications using the WCF API.
System.Workflow.Runtime These are two of many namespaces that define types used
System.Workflow.Activities to build “workflow-enabled” applications using the WCF API.
System.Threading This namespace defines numerous types to build
multithreaded applications.
System.Security Security is an integrated aspect of the .NET universe. In the
security-centric namespaces, you find numerous types dealing with
permissions, cryptography, and so on.
System.Xml The XML-centric namespaces contain numerous types used
to interact with XML data.

Mrs. Reena Vimal A. G.


Assistant Professor, MCA Dept.
TOCE
Compilation to managed code
The .NET Framework requires that you use a language compiler that is targeted at the CLR, such as
the Visual Basic .NET, C#, C++ .NET, or JScript .NET compilers provided by Microsoft. The figure
1given below shows the compilation process. After using one of the language compilers, your code is
compiled down to Microsoft Intermediate Language. Microsoft Intermediate Language, known as
MSIL or simply IL, is a CPU-independent set of instructions that can be easily converted to native
code. The metadata is also contained within the IL.

The IL is CPU-independent. This means that IL code is not reliant on the specific computer that
generated it. In other words, it can be moved from one computer to another without any complications.
After IL, the code that you started with will be compiled down even further by the JIT compiler to
machine code or native code. The IL contains everything that is needed to do this, such as the
instructions to load and call methods and a number of other operations.

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.

.NET compilers function a little differently, as they do not target a specific native processor. Instead,
they consume source files and produce binary files containing an intermediate representation of the
source constructs, expressed as a combination of metadata and Common Intermediate Language
(CIL). In order for these binaries to be executed, the CLR must be present on the target machine.

Mrs. Reena Vimal A. G.


Assistant Professor, MCA Dept.
TOCE
When these binaries are executed, they cause the CLR to load. The CLR then takes over and manages
execution, providing a range of services such as JIT compilation (converting the CIL as needed into
the correct stream of instructions for the underlying processor), memory management (in the form of a
garbage collector), exception management, debugger and profiler integration, and security services
(stack walking and permission checks).

This compilation and execution model explains why C# is referred to as a managed language, why
code running in the CLR is referred to as managed code, and why the CLR is said to provide managed
execution.

MSCorEE.dll (Microsoft Component object runtime Execution Engine)

The executable code in the assembly is compiled by the JIT compiler as it is needed. It is then cached
in case it is needed for execution again later in the program. Using this process means that code that
isn’t called isn’t compiled to native code, and code that is called is only compiled once.

Once the CIL is compiled to native code, the CLR manages it as it runs, performing such tasks as
releasing orphaned memory, checking array bounds, checking parameter types, and managing
exceptions. This brings up two important terms:

• Managed code: Code written for the .NET Framework is called managed code, and needs the
CLR.
• Unmanaged code: Code that does not run under the control of the CLR, such as Win32 C/C++
DLLs, is called unmanaged code.

Mrs. Reena Vimal A. G.


Assistant Professor, MCA Dept.
TOCE

You might also like