Introducing the .NET-0 AFramework 4.0
Introducing the .NET-0 AFramework 4.0
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.
.NET Framework so that you will know how applications built with Visual Basic 2010 can
run and how they can be built.
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.
1
FIGURE 1.1 The .NET Framework 4.0 architecture.
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.
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
.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.
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 C# 4.0
. Visual F# 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
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.
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.
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:
. 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
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.
The file Hello.exe will be created, which you can now run.
>hello
Hello World!
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
Visual Studio.NET
.NET Framework Class Library
Web Services
Windows UI
and Web UI
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.
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.
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.
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).
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
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.
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
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.
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.