0% found this document useful (0 votes)
6 views

Introducing the .NET-0 AFramework 4.0

This chapter introduces the Microsoft .NET Framework 4.0, which serves as the infrastructure for building and managing applications. It covers key components such as the Common Language Runtime (CLR), the Base Class Library (BCL), and the various programming languages supported by .NET, including Visual Basic 2010 and C#. Additionally, it highlights the differences from previous versions and the tools available for developers using Visual Studio 2010.

Uploaded by

JINESH VARIA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Introducing the .NET-0 AFramework 4.0

This chapter introduces the Microsoft .NET Framework 4.0, which serves as the infrastructure for building and managing applications. It covers key components such as the Common Language Runtime (CLR), the Base Class Library (BCL), and the various programming languages supported by .NET, including Visual Basic 2010 and C#. Additionally, it highlights the differences from previous versions and the tools available for developers using Visual Studio 2010.

Uploaded by

JINESH VARIA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

IN THIS CHAPTER

. What Is the .NET Framework?


Introducing the .NET . The Common Language
Runtime
Framework 4.0 . The Base Class Library
. .NET Languages
. .NET Framework Tools

As a Visual Basic 2010 developer, you need to understand . What’s New in .NET
Framework 4.0
the concepts and technology that empower your applica-
tions: the Microsoft .NET Framework. The .NET Framework
(also simply known as .NET) is the technology that provides
the infrastructure for building the next generation’s applica-
tions that you will create. Although covering every aspect
of the .NET Framework is not possible, in this chapter you
learn the basis of the .NET Framework architecture, why it
is not just a platform, and notions about the Base Class
Library and tools. The chapter also introduces important
concepts and terminology that will be of common use
throughout the rest of the book.

What Is the .NET Framework?


Microsoft .NET Framework is a complex technology that
provides the infrastructure for building, running, and
managing next generation applications. In a layered repre-
sentation, the .NET Framework is a layer positioned
between the Microsoft Windows operating system and your
applications. .NET is a platform but also is defined as a
technology because it is composed of several parts such as
libraries, executable tools, and relationships and integrates
with the operating system. Microsoft Visual Studio 2010
relies on the new version of the .NET Framework 4.0. Visual
Basic 2010, C# 4.0, and F# 2010 are .NET languages that
rely on and can build applications for the .NET Framework
4.0. The new version of this technology introduces impor-
tant new features that will be described later. In this chapter
you get an overview of the most important features of the
2 Introducing the .NET Framework 4.0

.NET Framework so that you will know how applications built with Visual Basic 2010 can
run and how they can be built.

Where Is the .NET Framework


When you install Microsoft Visual Studio 2010, the setup process installs the .NET Framework
4.0. .NET is installed to a folder named %windir%\Microsoft.NET\Framework\4.0. If you
open this folder with Windows Explorer, you see a lot of subfolders, libraries, and
executable tools. Most of the DLL libraries constitute the Base Class Library, whereas most
of the executable tools are invoked by Visual Studio 2010 to perform different kinds of
tasks, even if they can also be invoked from the command line. Later in this chapter we
describe the Base Class Library and provide an overview of the tools; for now you need to
notice the presence of a file named Vbc.exe, which is the Visual Basic Compiler and a
command line tool. In most cases you do not need to manually invoke the Visual Basic
compiler, because you will build your Visual Basic applications writing code inside Visual
Studio 2010, and the IDE invokes the compiler for you. But it is worth mentioning that
you could create the most complex application using Windows’s Notepad and then run
Vbc. Finally, it is also worth mentioning that users can get the .NET Framework 4.0 from
Microsoft for free. This means that the Visual Basic compiler is also provided free with
.NET, and this is the philosophy that characterizes the .NET development since the first
version was released in 2002.

The .NET Framework Architecture


To better understand the structure of the .NET Framework, think about it as a layered
architecture. Figure 1.1 shows a high-level representation of the .NET Framework 4.0
architecture.

The first level of the representation is the operating system; the .NET layer is located
between the system and applications. The second level is the Common Language Runtime
(CLR), which provides the part of the .NET Framework doing the most work. We discuss
the CLR later in this chapter. The next level is the Base Class Library (BCL), which provides
all .NET objects that can be used both in your code and by Visual Basic when creating
applications. The BCL also provides the infrastructure of several .NET technologies that
you use in building applications, such as WPF, Windows Forms, ASP.NET, WCF, and so on.
The last level is represented by applications that rely on the previous layers.

DIFFERENCES WITH PREVIOUS VERSIONS


If you upgrade to Visual Basic 2010 from Visual Basic 2008, the main difference that
you notice is that .NET 4.0 is a standalone infrastructure. You may remember that .NET
Framework 3.5 was instead an incremental framework that needed the prior installation
of .NET 2.0 and .NET 3.0. For example, LINQ was part of .NET 3.5 whereas WPF was
part of .NET 3.0 and Windows Forms was part of .NET 2.0 (see Figure 1.2 for a graphi-
cal representation). With .NET 4.0 this incremental structure disappears, and all the
frameworks, BCL, and tools are part of the new version.
What Is the .NET Framework? 3

1
FIGURE 1.1 The .NET Framework 4.0 architecture.

FIGURE 1.2 The incremental architecture of the .NET Framework 3.5 SP 1.


4 Introducing the .NET Framework 4.0

Although the various frameworks exposed by the BCL are discussed later in the book, in this
chapter, now you get an overview of the library and can understand how it works and how
you can use it. But before examining the BCL, consider the Common Language Runtime.

The Common Language Runtime


As its name implies, the Common Language Runtime provides an infrastructure that is
common to all .NET languages. This infrastructure is responsible for taking control of the
application’s execution and manages tasks such as memory management, access to system
resources, security services, and so on. This kind of common infrastructure bridges the gap
that exists between different Win32 programming languages because all .NET languages
have the same possibilities. Moreover, the Common Language Runtime enables applica-
tions to run inside a managed environment. The word managed is fundamental in the
.NET development, as explained in next paragraph.

Writing Managed Code


When talking about Visual Basic 2010 development and, more generally, about .NET
development, you often hear about writing managed code. Before the first version of .NET
(or still with non-.NET development environments), the developer was the only responsi-
ble person for interacting with system resources and the operating system. For example,
taking care of accessing parts of the operating system or managing memory allocation for
objects were tasks that the developer had to consider. In other words, the applications
could interact directly with the system, but as you can easily understand this approach
has some big limitations both because of security issues and because damages could be
dangerous. The .NET Framework provides instead a managed environment. This means
that the application communicates with the .NET Framework instead of with the operat-
ing system, and the .NET runtime is responsible for managing the application execution,
including memory management, resources management, and access to system resources.
For example, the Common Language Runtime could prevent an application from access-
ing particular system resources if it is not considered full-trusted according to the Security
Zones of .NET.

DIALING WITH THE SYSTEM


You can still interact directly with the operating system, for example invoking Windows
APIs (also known as Platform Invoke or P/Invoke for short). This technique is known as
writing unmanaged code that should be used only when strictly required. This topic is
discussed in Chapter 49, “Platform Invokes and Interoperability with the COM
Architecture.”

Writing managed code and the existence of the Common Language Runtime also affect
how applications are produced by compilers.
01_0672331004_ch01.qxp 5/3/10 5:40 PM Page 5

The Base Class Library 5

.NET Assemblies
In classic Win32 development environments, such as Visual Basic 6 or Visual C++, your

1
source code is parsed by compilers that produce binary executable files that can be imme-
diately interpreted and run by the operating system. This affects both standalone applica-
tions and dynamic/type libraries. Actually Win32 applications, built with Visual Basic 6
and C++, used a runtime, but if you had applications developed with different program-
ming languages, you also had to install the appropriate runtimes. In.NET development
things are quite different. Whatever .NET language you create applications with, compilers
generate an assembly, which is a file containing .NET executable code and is composed
essentially by two kinds of elements: MSIL code and metadata. MSIL stands for Microsoft
Intermediate Language and is a high-level assembly programming language that is also
object-oriented, providing a set of instructions that are CPU-independent (rather than
building executables that implement CPU-dependent sets of instructions). MSIL is a
common language in the sense that the same programming tasks written with different
.NET languages produce the same IL code. Metadata is instead a set of information related
to the types implemented in the code. Such information can contain signatures, functions
and procedures, members in types, and members in externally referenced types. Basically
metadata’s purpose is describing the code to the .NET Framework. Obviously, although an
assembly can have .exe extension, due to the described structure, it cannot be directly
executed by the operating system. In fact, when you run a .NET application the operating
system can recognize it as a .NET assembly (because between .NET and Windows there is a
strict cooperation) and invoke the Just-In-Time compiler.

The Execution Process and the Just-In-Time (JIT) Compiler


.NET compilers produce assemblies that store IL code and metadata. When you launch an
assembly for execution, the .NET Framework packages all the information and translates
them into an executable that the operating system can understand and run. This task is
the responsibility of the Just-In-Time (JIT) compiler. JIT compiles code on-the-fly just
before its execution and keeps the compiled code ready for execution. It acts at the
method level. This means that it first searches for the application’s entry point (typically
the Sub Main) and then compiles other procedures or functions (methods in .NET termi-
nology) referenced and invoked by the entry point and so on, just before the code is
executed. If you have some code defined inside external assemblies, just before the
method is executed the JIT compiler loads the assembly in memory and then compiles the
code. Of course loading an external assembly in memory could require some time and
affect performance, but it can be a good idea to place seldom-used methods inside exter-
nal assemblies, the same way as it could be a good idea to place seldom-used code inside
separated methods.

The Base Class Library


The .NET Framework Base Class Library (BCL) provides thousands of reusable types that
you can use in your code and that cover all the .NET technologies, such as Windows
Presentation Foundation, ASP.NET, LINQ, and so on. Types defined in the Base Class
Library enable developers to do millions of things without the need of calling unmanaged
6 Introducing the .NET Framework 4.0

code and Windows APIs and, often, without recurring to external components. A type is
something that states what an object must represent. For example, String and Integer are
types, and you might have a variable of type String (that is, a text message) or a variable
of type Integer (a number). Saying Type is not the same as saying Class. In fact, types can
be of two kinds: reference types and value types. This topic is the specific subject of Chapter
4, “Data Types and Expressions”—a class is just a reference type. Types in the BCL are
organized within namespaces, which act like a kind of types’ containers, and their name is
strictly related to the technology they refer to. For example, the System.Windows.Forms
namespace implements types for working with Windows Forms applications, whereas
System.Web implements types for working with Web applications, and so on. You will get
a more detailed introduction to namespaces in Chapter 3, “The Anatomy of a Visual Basic
Project,” and Chapter 9, “Organizing Types Within Namespaces.” Basically each name-
space name beginning with System is part of the BCL. There are also some namespaces
whose name begins with Microsoft that are still part of the BCL. These namespaces are
typically used by the Visual Studio development environment and by the Visual Basic
compiler, although you can also use them in your code in some particular scenarios (such
as code generation).

The BCL is composed of several assemblies. One of the most important is MsCorlib.dll
(Microsoft Core Library) that is part of the .NET Framework and that will always be
required in your projects. Other assemblies can often be related to specific technologies;
for example, the System.ServiceModel.dll assembly integrates the BCL with the Windows
Communication Foundation main infrastructure. Also, some namespaces don’t provide
the infrastructure for other technologies and are used only in particular scenarios; there-
fore, they are defined in assemblies external from MsCorlib (Microsoft Core Library). All
these assemblies and namespaces will be described in the appropriate chapters.

.NET Languages
Microsoft offers several programming languages for the .NET Framework 4.0. With
Visual Studio 2010, you can develop applications with the following integrated
programming languages:

. Visual Basic 2010

. Visual C# 4.0

. Visual F# 2010

. Visual C++ 2010

Visual J# is no longer part of the .NET Framework family. You can also integrate native
languages with Microsoft implementations of Python and Ruby dynamic languages,
respectively known as IronPython and IronRuby.
.NET Framework Tools 7

WHERE DO I FIND IRONPYTHON AND IRONRUBY?


IronPython and IronRuby are currently under development by Microsoft and are available

1
as open source projects from the CodePlex community. You can download IronPython
from http://ironpython.codeplex.com. You can find IronRuby at http://ironruby.code-
plex.com.

There are also several third-party implementations of famous programming languages for
.NET, such as Fortran, Forth, or Pascal, but discussing them is neither a purpose of this
chapter nor of this book. It’s instead important to know that all these languages can take
advantage of the .NET Framework base class library and infrastructure the same as VB and
C#. This is possible because of the Common Language Runtime that offers a common
infrastructure for all .NET programming languages.

.NET Framework Tools


The .NET Framework also provides several command-line tools needed when creating
applications. Among the tools are the compilers for the .NET languages, such as Vbc.exe
(Visual Basic compiler), Csc.exe (Visual C# compiler), and MSBuild.exe (the build engine
for Visual Studio). All these tools are stored in the C:\Windows\Microsoft.NET\
Framework\v4.0 folder. In most scenarios you will not need to manually invoke the .NET
Framework tools, because you will work with the Microsoft Visual Studio 2010 Integrated
Development Environment, which is responsible for invoking the appropriate tools when
needed. Instead of listing all the tools now, because we have not talked about some topics
yet, information on the .NET tools invoked by Visual Studio is provided when discussing a
particular topic that involves the specific tools.

Windows Software Development Kit


Starting from Visual Studio 2008, with the .NET Framework and the development environ-
ment, the setup process will also install the Windows SDK on your machine. This software
development kit provides additional tools and libraries useful for developing applications
for the .NET Framework. In older versions of the .NET Framework and in the Microsoft
Windows operating system, you had to install two different packages, formerly known as
the .NET Framework SDK and the Microsoft Platform SDK. With the introduction of
Windows Vista, the .NET Framework has become part of the core of the operating system;
Microsoft released the Windows SDK that provides tools for building both managed and
unmanaged applications. The Windows SDK is installed into the C:\Program
Files\Microsoft SDKs\Windows\v7.0A folder, which includes several additional tools also
used by Microsoft Visual Studio for tasks different from building assemblies, such as
8 Introducing the .NET Framework 4.0

deployment and code analysis, or for generating proxy classes for Windows
Communication Foundation projects. Also in this case you will not typically need to
invoke these tools manually, because Visual Studio will do the work for you. You can find
information on the Windows SDK’s tools in the appropriate chapters.

What’s New in .NET Framework 4.0


If you had development experiences with .NET Framework 3.5, you know that it has an
incremental architecture. This means that .NET 3.5 (including technologies typical of this
version such as LINQ) relies on .NET Framework 2.0 for most of the core .NET features and
technologies such as Windows Forms, whereas it requires .NET Framework 3.0 for frame-
works such as Windows Presentation Foundation, Windows Communication Foundation,
Windows Workflow Foundation, and CardSpace. This means that .NET Framework 3.5
requires previous versions to be installed as a prerequisite. The .NET Framework 4.0 is
instead a complete standalone technology that does not require other previous versions to
be installed. Assuming you have some knowledge of .NET Framework 3.0 and 3.5, follow-
ing are new technologies introduced by .NET 4.0 for your convenience:

. Windows Presentation Foundation

. Windows Communication Foundation

. ASP.NET (now including Ajax and MVC)

. ADO.NET Entity Framework

. Visual Studio Tools for Office

. Windows Workflow Foundation

The new version of these technologies is not just an addition of features, but the architec-
ture has been revised and improved. The .NET Framework 4.0 also includes some frame-
works that in the previous version had to be installed manually or as part of the .NET 3.5
Service Pack 1:

. ADO.NET Data Services

. Parallel Extensions for the Task Parallel Library, or TPL for short (related to the
parallel computing)

. Code Contracts

The Windows Forms technology is still unchanged from .NET Framework 2.0. There are
just a few additions regarding user controls, which is discussed in Chapter 30. “Creating
Windows Forms 4.0 Applications.”
Summary 9

Summary
Understanding the .NET Framework is of primary importance in developing applications

1
with Visual Basic 2010 because you will build applications for the .NET Framework. This
chapter presented a high-level overview of the .NET Framework 4.0 and key concepts such
as the Common Language Runtime and the Base Class Library and how an application is
compiled and executed. You also got an overview of the most important command-line
tools and the .NET languages.
O N E
1
.NET Framework

Microsoft’s popular programming language, Visual Basic, has


been a favorite choice of programmers for many years. The ease
with which Windows applications may be built, coupled with its
wealth of database capabilities, has entrenched it in the hearts of
many programmers. In its latest version, Microsoft has revamped
Visual Basic, now called Visual Basic.NET (or simply VB.NET), to
include full object-oriented capabilities and has provided support
to work with the .NET Framework. We examine some of these
issues throughout the book as we learn of the powerful services
that are provided to the VB.NET programmer by the .NET Frame-
work. In this chapter, we introduce the .NET Framework in suffi-
cient detail so that you can immediately begin programming in
VB.NET. For more in-depth information about .NET, you can refer
to other books in The Integrated .NET Series from Object Innova-
tions and Prentice Hall PTR. Of particular interest to VB.NET pro-
grammers will be Application Development Using Visual Basic and
.NET (Oberg, Thorsteinson, Wyatt), which delves into many
important topics that are beyond the scope of this book.

.NET: What You Need to Know


A beautiful thing about .NET is that, from a programmer’s perspective, you
scarcely need to know anything about it to start writing programs for the .NET
environment. You write a program in a high-level language such as VB.NET, a

3
4 G .NET Framework

compiler creates an executable (.EXE) file, and you run that EXE file. We
show you exactly how to do that in just a few pages. Naturally, as the scope
of what you want to do expands, you will need to know more. But to get
started, you need to know very little.
Even very simple programs, if they perform any input or output, will
generally require the use of the services found in library code. A large library,
called the .NET Framework Class Library, comes with .NET, and you can use
all of the services of this library in your programs.
What is really happening in a .NET program is somewhat elaborate. The
EXE file that is created does not contain executable code, but rather Interme-
diate Language code, or IL (sometimes called Microsoft Intermediate Lan-
guage or MSIL). In the Windows environment, this IL code is packaged up in
a standard portable executable (PE) file format, so you will see the familiar
EXE extension (or, if you are building a component, the DLL extension).
When you run the EXE, a special runtime environment (the Common Lan-
guage Runtime or CLR) is launched, and the IL instructions are executed by
the CLR. Unlike some runtimes, where the IL would be interpreted each time
it is executed, the CLR comes with a just-in-time (JIT) compiler that translates
the IL to native machine code the first time it is encountered. On subsequent
calls, the code segment runs as native code.
Thus, in a nutshell, the process of programming in the .NET environ-
ment goes like this:
1. Write your program in a high-level .NET language such as VB.NET.
2. Compile your program into IL.
3. Run your IL program, which launches the CLR to execute your IL, using
its JIT to translate your program into native code as it executes.

Installing the .NET SDK


All you need to compile and run the programs in this book is the .NET Frame-
work SDK. This SDK is available on CD, or it can be downloaded for free
from the Microsoft .NET Web site at http://msdn.microsoft.com/net/. Follow
the installation directions for the SDK, and make sure that your computer
meets the hardware requirements. (Generally, for the SDK, you need a fast
Pentium processor and at least 128M of RAM.) Part of the installation is a Win-
dows Component Update, which will update your system, if necessary, to
recent versions of programs such as Internet Explorer. The SDK will install
tools such as compilers, documentation, sample programs, and the CLR.
The starting place for the SDK documentation is the .NET Framework
SDK Overview (see Figure 1–1).
.NET: What You Need to Know 5

FIGURE 1–1 Homepage of .NET Framework SDK.

Installing the Book Software


The example programs found in this book are available on the Web at http://
www.objectinnovations.com/dotnet.htm/. Download the file Install_IntroVb.exe
and run this self-extracting file. If you accept the suggested installation directory,
the software will be installed in the directory OI\IntroVb on your C: drive. There
are subdirectories for each chapter of the book. The directory for Chapter 1 is
Chap01. Sample programs are in named subdirectories of the chapter directory,
and we will refer to these sample programs simply by name, such as Hello.

Your First VB.NET Program


Although we won’t actually start to examine the structure of VB.NET pro-
grams until Chapter 2, you don’t have to wait to compile and run your first
VB.NET program. Start at the command prompt, and navigate to the Hello
directory for this chapter. (If you accepted the default installation, the direc-
tory is C:\OI\IntroVb\Chap01\Hello.) The source file is Hello.vb. To com-
pile this program, enter the following command:
>vbc hello.vb
6 G .NET Framework

The file Hello.exe will be created, which you can now run.
>hello
Hello World!

Setting Environment Variable


In order to run command line tools such as the VB.NET compiler using the name vbc rather
than the complete path, certain environment variables must be set. The environment vari-
ables can be set using the batch file vsvars32.bat, which can be found in the Common\Tools
directory of the Framework SDK.
If you have Visual Studio.NET installed, you can ensure that the environment variables are
set up by starting your command prompt session from Start | Programs | Microsoft Visual
Studio.NET 7.0 | Microsoft Visual Studio Tools | Microsoft Visual Studio.NET Command
Prompt.

Visual Studio.NET
Although the .NET Framework SDK is all you need to compile and run
VB.NET programs, the process will be much easier and more pleasant if you
use the Visual Studio.NET integrated development environment (IDE). The
IDE provides an easy-to-use editor, access to the compiler and debugger, and
access to online help. We will discuss Visual Studio.NET in Chapter 3.

Understanding .NET
If you are eager to start learning the VB.NET programming language right
away, by all means proceed directly to Chapter 2. The nice thing about a
high-level programming language is that, for the most part, you do not need
to be concerned with the platform on which the program executes (unless
you are making use of services provided by the platform). You can work with
the abstractions provided by the language and with functions provided by
libraries.
However, you will better appreciate the VB.NET programming language
and its potential for creating sophisticated applications if you have a general
understanding of .NET. The rest of this chapter is concerned with helping you
to achieve such an understanding. We address three broad topics:
G What Is Microsoft .NET?

G .NET Framework

G Common Language Runtime


What Is Microsoft .NET? 7

What Is Microsoft .NET?


In this section, we answer the high-level question “What is .NET?” In brief,
.NET represents Microsoft’s vision of the future of applications in the Internet
age. .NET provides enhanced interoperability features based upon open Inter-
net standards.
The classic Windows desktop has been plagued by robustness issues.
.NET represents a great improvement. For developers, .NET offers a new pro-
gramming platform and superb tools.
XML plays a fundamental role in .NET. Enterprise servers (such as SQL
2000) expose .NET features through XML.
Microsoft .NET is a new platform at a higher level than the operating
system. Three years in the making before public announcement, .NET is a
major investment by Microsoft. .NET draws on many important ideas, includ-
ing XML, the concepts underlying Java, and Microsoft’s Component Object
Model (COM). Microsoft .NET provides the following:
G A robust runtime platform, the CLR
G Multiple language development
G An extensible programming model, the .NET Framework, which pro-
vides a large class library of reusable code available from multiple
languages
G A networking infrastructure built on top of Internet standards that
supports a high level of communication among applications
G A new mechanism of application delivery, the Web service, that sup-
ports the concept of an application as a service
G Powerful development tools

Microsoft and the Web


The World Wide Web has been a big challenge to Microsoft. It did not
embrace it early. But the Web actually coexists quite well with Microsoft’s tra-
ditional strength, the PC. Using the PC’s browser application, a user can gain
access to a whole world of information. The Web relies on standards such as
HTML, HTTP, and XML, which are essential for communication among diverse
users on a variety of computer systems and devices.
The Windows PC and the Internet, although complex, are quite stan-
dardized. However, a Tower of Babel exists with respect to the applications
that try to build on top of them: multiple languages, databases, and develop-
ment environments. The rapid introduction of new technologies has created a
gap in the knowledge of workers who must build systems using these tech-
nologies. This provides an opening for Microsoft, and some of the most talked
about parts of .NET are indeed directed toward the Internet.
8 G .NET Framework

.NET provides many features to greatly enhance our ability to program


Web applications, but this topic is beyond the scope of this book. For more
information, please consult the following two books in The Integrated .NET
Series:
G Application Development Using Visual Basic and .NET (Oberg,
Thorsteinson, Wyatt)
G Fundamentals of Web Applications Using .NET and XML (Bell, Feng,
Soong, Zhang, Zhu)

Windows on the Desktop


Microsoft began with the desktop, and the company has achieved much. The
modern Windows environment has become ubiquitous. Countless applica-
tions are available, and most computer users are at least somewhat at home
with Windows. There is quite a rich user interface experience, and applica-
tions can work together. But there are also significant problems.

PROBLEMS WITH WINDOWS


One of the most troublesome problems is the maintenance of applications on
the Windows PC. Applications consist of many files, registry entries, shortcuts,
and so on. Different applications can share certain DLLs. Installing a new
application can overwrite a DLL that an existing application depends on, pos-
sibly breaking the older application (which is known as “DLL hell”). Remov-
ing an application is complex and often is imperfectly done. Over time, a PC
can become less stable, and the cure eventually becomes reformatting the
hard disk and starting from scratch.
There is tremendous economic benefit to using PCs, because standard
applications are inexpensive and powerful, the hardware is cheap, and so on.
But the savings are reduced by the cost of maintenance.

A ROBUST WINDOWS ENVIRONMENT


.NET has many features that will result in a much more robust Windows oper-
ating system. Applications no longer rely on storing extensive configuration
data in the registry. In .NET, applications are self-describing, containing meta-
data within the program executable files themselves. Different versions of an
application can be deployed side-by-side.
Applications run managed code. Managed code is not executed directly
by the operating system, but rather by the special runtime—the CLR. The CLR
can perform checks for type safety, such as for array out-of-bounds and mem-
ory overwrites. The CLR performs memory management, including automatic
garbage collection, resulting in sharp reduction of memory leaks and similar
problems.
.NET Framework Overview 9

Languages such as VB.NET and C# (pronounced “C sharp”), but not


C++, can produce managed code that is verifiably secure. Managed code that
is not verifiable can run if the security policy allows the code to ignore the
verification process.

A New Programming Platform


.NET provides a new programming platform at a higher level than the operat-
ing system. This level of abstraction has many advantages:
G Code can be validated to prevent unauthorized actions
G It is much easier to program than the Win32 API or COM
G All or parts of the platform can be implemented on many different
kinds of computers (as has been done with Java)
G All the languages use one class library
G Languages can interoperate with each other
We outline the features of this new platform, the .NET Framework, in the
next section.

.NET Framework Overview


The .NET Framework consists of the CLR, the .NET Framework Class Library,
the Common Language Specification (CLS), a number of .NET languages, and
Visual Studio.NET. The overall architecture of the .NET Framework is depicted
in Figure 1–2.

C# VB.NET C++ Other

Common Language Specification

Visual Studio.NET
.NET Framework Class Library

Common Language Runtime

FIGURE 1–2 Overall block diagram of .NET Framework.


10 G .NET Framework

Common Language Runtime


A runtime provides services to executing programs. Traditionally, different
programming environments have different runtimes. Examples of runtimes
include the standard C library, MFC, the Visual Basic runtime, and the Java
Virtual Machine (JVM).
The runtime environment provided by .NET, the CLR, manages the exe-
cution of code and provides useful services. The services of the CLR are
exposed through programming languages. The syntax for these services varies
from language to language, but the underlying execution engine providing
the services is the same.
Not all languages expose all the features of the CLR. The language with
the best mapping to the CLR is the new language C#. VB.NET, however, does
an admirable job of exposing the functionality.

.NET Framework Class Library


The .NET Framework class library is huge, comprising more than 2,500
classes. All this functionality is available to all the .NET languages. The library
(see Figure 1–3) consists of four main parts:
1. Base class library (which includes networking, security, diagnostics, I/O,
and other types of operating system services)
2. Data and XML classes
3. Windows UI
4. Web services and Web UI

Web Services
Windows UI
and Web UI

Data and XML

Base Class Library

FIGURE 1–3 Block diagram of .NET Framework Class Library.


Common Language Runtime 11

Common Language Specification


An important goal of the .NET Framework is to support multiple languages.
But all languages are not created equal, so it is important to agree upon a
common subset that all languages will support. The CLS is an agreement
among language designers and class library designers about those features
and usage conventions that can be relied upon.
CLS rules apply to public features that are visible outside the assembly
where they are defined. (An assembly can be thought of as a logical EXE or
DLL and will be discussed later in this chapter.) For example, the CLS requires
that public names do not rely on case for uniqueness, because some lan-
guages are not case sensitive. For more information, see “Cross Language
Interoperability” in “Inside the .NET Framework” in the .NET Framework SDK
documentation.

Languages in .NET
A language is a CLS-compliant consumer if it can use any CLS-compliant
type—that is, if it can call methods, create instances of types, and so on. (A
type is basically a class in most object-oriented languages, providing an
abstraction of data and behavior, grouped together.) A language is a CLS-com-
pliant extender if it is a consumer and can also extend any CLS-compliant
base class, implement any CLS-compliant interface, and so on.
Microsoft itself is providing four CLS-compliant languages. VB.NET, C#,
and C++ with Managed Extensions are extenders. JScript.NET is a consumer.
Third parties are providing additional languages (more than a dozen so
far). Active-State is implementing Perl and Python. Fujitsu is implementing
COBOL. It should be noted that at present some of these languages are not
.NET languages in the strict sense. For example, ActiveState provides a tool
called PerlNET that will create a .NET component from a Perl class. This facil-
ity enables .NET applications to call the wealth of Perl modules, but it does
not make Perl into either a consumer or an extender. For more information on
PerlNET, see the book Programming Perl in the .NET Environment (Saltzman,
Oberg), another book in The Integrated .NET Series.

Common Language Runtime


In this section, we delve more deeply into the structure of .NET by examining
the CLR. We look at the design goals of the CLR and discuss the rationale for
using managed code and a runtime. We outline the design of the CLR, includ-
ing the concepts of MSIL, metadata, and JIT compilation. We compare the CLR
with the Java Virtual Machine. We discuss the key concept in .NET of assem-
12 G .NET Framework

bly, which is a logical grouping of code. We explore the central role of types
in .NET and look at the Common Type System (CTS). We explain the role of
managed data and garbage collection. Finally, we use the Intermediate Lan-
guage Disassembler (ILDASM) tool to gain some insight into the structure of
assemblies.

Design Goals of the CLR


The CLR has the following design goals:
G Simplify application development
G Support multiple programming languages
G Provide a safe and reliable execution environment
G Simplify deployment and administration
G Provide good performance and scalability

SIMPLE APPLICATION DEVELOPMENT


With more than 2,500 classes, the .NET Framework class library provides
enormous functionality that the programmer can reuse. The object-oriented
and component features of .NET enable organizations to create their own
reusable code. Unlike COM, the programmer does not have to implement any
plumbing code to gain the advantages of components. Automatic garbage col-
lection greatly simplifies memory management in applications. The CLR facili-
tates powerful tools such as Visual Studio.NET that can provide common
functionality and the same UI for multiple languages.

MULTIPLE LANGUAGES
The CLR was designed from the ground up to support multiple languages.
This feature is the most significant difference between .NET and Java, which
share a great deal in philosophy.
The CTS makes interoperability between languages virtually seamless.
The same built-in data types can be used in multiple languages. Classes
defined in one language can be used in another language. A class in one lan-
guage can even inherit from a class in another language. Exceptions can be
thrown from one language to another.
Programmers do not have to learn a new language in order to use .NET.
The same tools can work for all .NET languages. You can debug from one
language into another.

SAFE EXECUTION ENVIRONMENT


With the CLR, a compiler generates MSIL instructions, not native code. It is
this managed code that runs. Hence, the CLR can perform runtime validations
on this code before it is translated into native code. Types are verified. Sub-
Common Language Runtime 13

scripts are verified to be in range. Unsafe casts and uninitialized variables are
prevented.
The CLR performs memory management. Managed code cannot access
memory directly. No pointers are allowed. This means that your code cannot
inadvertently write over memory that does not belong to it, possibly causing a
crash or other bad behavior.
The CLR can enforce strong security. One of the challenges of the soft-
ware world of third party components and downloadable code is that you
open your system to damage from executing code from unknown sources.
You might want to restrict Word macros from accessing anything other than
the document that contains them. You want to stop potentially malicious Web
scripts. You even want to shield your system from bugs of software from
known vendors. To handle these situations, .NET security includes Code
Access Security (CAS).

SIMPLER DEPLOYMENT AND ADMINISTRATION


With the CLR, the unit of deployment becomes an assembly, which is typically
an EXE or a DLL. The assembly contains a manifest, which allows much more
information to be stored.
An assembly is completely self-describing. No information needs to be
stored in the registry. All the information is in one place, and the code cannot
get out of sync with information stored elsewhere, such as in the registry, a
type library, or a header file.
The assembly is the unit of versioning, so that multiple versions can be
deployed side by side in different folders. These different versions can exe-
cute at the same time without interfering with each other.
Assemblies can be private or shared. For private assembly deployment,
the assembly is copied to the same directory as the client program that refer-
ences it. No registration is needed, and no fancy installation program is
required. When the component is removed, no registry cleanup is needed,
and no uninstall program is required. Just delete it from the hard drive.
In shared assembly deployment, an assembly is installed in the Global
Assembly Cache (or GAC). The GAC contains shared assemblies that are glo-
bally accessible to all .NET applications on the machine. A download assem-
bly cache is accessible to applications such as Internet Explorer that
automatically download assemblies over the network.

PERFORMANCE
You may like the safety and ease-of-use features of managed code, but you
may be concerned about performance. It is somewhat analogous to the con-
cerns of early assembly language programmers when high-level languages
came out.
14 G .NET Framework

The CLR is designed with high performance in mind. JIT compilation is


designed into the CLR. The first time a method is encountered, the CLR per-
forms verifications and then compiles the method into native code (which will
contain safety features, such as array bounds checking). The next time the
method is encountered, the native code executes directly.
Memory management is designed for high performance. Allocation is
almost instantaneous, just taking the next available storage from the managed
heap. Deallocation is done by the garbage collector, which Microsoft has
tweaked for efficiency.

Why Use a CLR?


Why did Microsoft create a CLR for .NET? Let’s look at how well the goals just
discussed could have been achieved without a CLR, focusing on the two main
goals of safety and performance. Basically, there are two philosophies. The
first is compile-time checking and fast native code at runtime. The second is
runtime checking.
Without a CLR, we must rely on the compiler to achieve safety. This
places a high burden on the compiler. Typically, there are many compilers for
a system, including third-party compilers. It is not robust to trust that every
compiler from every vendor will adequately perform all safety checking. Not
every language has features supporting adequate safety checking. Compila-
tion speed is slow with complex compilation. Compilers cannot conveniently
optimize code based on enhanced instructions available on some platforms
but not on others. What’s more, many features (such as security) cannot be
detected until runtime.

Design of Common Language Runtime


So we want a runtime. How do we design it? One extreme is to use an inter-
preter and not a compiler at all. All the work is done at runtime. We have
safety and fast builds, but runtime performance is very slow. Modern systems
divide the load between the front-end compiler and the back-end runtime.

INTERMEDIATE LANGUAGE
The front-end compiler does all the checking it can do and generates an inter-
mediate language. Examples include
G P-code for Pascal
G Bytecode for Java
The runtime does further verification based on the actual runtime char-
acteristics, including security checking.
With JIT compilation, native code can be generated when needed and
subsequently reused. Runtime performance becomes much better. The native
Common Language Runtime 15

code generated by the runtime can be more efficient, because the runtime
knows the precise characteristics of the target machine.

MICROSOFT INTERMEDIATE LANGUAGE


All managed code compilers for Microsoft .NET generate MSIL. MSIL is
machine-independent and can be efficiently compiled into native code.
MSIL has a wide variety of instructions:
G Standard operations such as load, store, arithmetic and logic, branch,
etc.
G Calling methods on objects
G Exceptions
Before executing on a CPU, MSIL must be translated by a JIT compiler.
There is a JIT compiler for each machine architecture supported. The same
MSIL will run on any supported machine.

METADATA
Besides generating MSIL, a managed code compiler emits metadata. Metadata
contains very complete information about the code module, including the fol-
lowing:
G Version and locale information
G All the types
G Details about each type, including name, visibility, etc.
G Details about the members of each type, such as methods, the signa-
tures of methods, etc.

Types
Types are at the heart of the programming model for the CLR. A type is analogous to a class
in most object-oriented programming languages, providing an abstraction of data and behav-
ior, grouped together. A type in the CLR contains the following:
G Fields (data members)

G Methods

G Properties

G Events

There are also built-in primitive types, such as integer and floating point numeric types,
strings, etc. In the CLR, there are no functions outside of types, but all behavior is provided
via methods or other members. We discuss types under the guise of classes and value types
when we cover VB.NET.

Metadata is the “glue” that binds together the executing code, the CLR,
and tools such as compilers, debuggers, and browsers. On Windows, MSIL
16 G .NET Framework

and metadata are packaged together in a standard Windows PE file. Metadata


enables “Intellisense” in Visual Studio. In .NET, you can call from one lan-
guage to another, and metadata enables types to be converted transparently.
Metadata is ubiquitous in the .NET environment.

JIT COMPILATION
Before executing on the target machine, MSIL is translated by a JIT compiler
to native code. Some code typically will never be executed during a program
run. Hence, it may be more efficient to translate MSIL as needed during exe-
cution, storing the native code for reuse.
When a type is loaded, the loader attaches a stub to each method of the
type. On the first call, the stub passes control to the JIT, which translates to
native code and modifies the stub to save the address of the translated native
code. On subsequent calls to the method, the native code is called directly.
As part of JIT compilation, code goes through a verification process.
Type safety is verified, using both the MSIL and metadata. Security restrictions
are checked.

COMMON TYPE SYSTEM


At the heart of the CLR is the Common Type System (CTS). The CTS provides
a wide range of types and operations that are found in many programming
languages. The CTS is shared by the CLR and by compilers and other tools.
The CTS provides a framework for cross-language integration and
addresses a number of issues:
G Similar, but subtly different, types (for example, Integer is 16 bits in
VB6, but int is 32 bits in C++; strings in VB6 are represented as
BSTRs and in C++ as char pointers or a string class of some sort;
and so on)
G Limited code reuse (for example, you can’t define a new type in one
language and import it into another language)
G Inconsistent object models
Not all CTS types are available in all languages. The CLS establishes
rules that must be followed for cross-language integration, including which
types must be supported by a CLS-compliant language. Built-in types can be
accessed through the System class in the Base Class Library (BCL) and
through reserved keywords in the .NET languages.
In Chapter 4, we begin our discussion of data types with the simple data
types. We continue the discussion of types in Chapter 11, where we introduce
reference types such as class and interface. At all times, you should bear in
mind that there is a mapping between types in VB.NET, represented by key-
words, and the types defined by the CTS, as implemented by the CLR.
Summary 17

Managed Data and Garbage Collection


Managed code is only part of the story of the CLR. A significant simplification
of the programming model is provided through managed data. When an
application domain is initialized, the CLR reserves a contiguous block of stor-
age known as the managed heap. Allocation from the managed heap is
extremely fast. The next available space is simply returned, in contrast to the
C runtime, which must search its heap for space that is large enough.
Deallocation is not performed by the user program but by the CLR,
using a process known as garbage collection. The CLR tracks the use of mem-
ory allocated on the managed heap. When memory is low, or in response to
an explicit call from a program, the CLR “garbage collects” (or frees up all
unreferenced memory) and compacts the space that is now free into a large
contiguous block.

Summary
VB.NET does not exist in isolation, but has a close connection with the under-
lying .NET Framework. In this chapter, you received an orientation to the
overall architecture and features of .NET.
Microsoft .NET is a new platform that sits on top of the operating system
and provides many capabilities for building and deploying desktop and Web-
based applications. .NET has many features that will create a much more
robust Windows operating system.
The .NET Framework includes the Common Language Runtime (CLR),
the .NET Framework Class Library, the Common Type System (CTS), the .NET
languages, and Visual Studio.NET.
The CLR manages the execution of code and provides useful services.
The design goals of the CLR included simple application development, safety,
simple deployment, support of multiple languages, and good performance.
.NET uses managed code that runs in a safe environment under the CLR.
.NET compilers translate source code into Microsoft Intermediate Language
(MSIL), which is translated at runtime into native code by a just-in-time (JIT)
compiler.
An assembly is a grouping of types and resources that work together as
a logical unit. Types and the CTS are the heart of the CLR. Garbage collection
is used by the CLR to automatically reclaim unreferenced data.
In Chapter 2, we will take our first steps in VB.NET programming.

You might also like