Professional Documents
Culture Documents
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.
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.
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.
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.
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 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.
System.Resources
Provides classes that allow you to create and lt ifi
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.
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)
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).
• 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.
// 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.
class Interface_imp:Interface_demo
{
public void Show()
{
Console.WriteLine("Show() method Implemented");
}
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);
}
}
• 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.
• 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
• 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.
• 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.
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.
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.
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.