.NET Basics
Unlike native machine code programs, .NET programs require a special envi-
ronment in which they can be executed. This environment, which is called the
.NET Framework, acts as a sort of intermediary between .NET programs and
the rest of the world. The .NET Framework is basically the software execution
environment in which all .NET programs run, and it consists of two primary
components: the common language runtime (CLR) and the .NET class library.
The CLR is the environment that loads and verifies .NET assemblies and is
essentially a virtual machine inside which .NET programs are safely executed.
The class library is what .NET programs use in order to communicate with the
outside world. It is a class hierarchy that offers all kinds of services such as
user-interface services, networking, file I/O, string management, and so on.
Figure 12.2 illustrates the connection between the various components that
together make up the .NET platform.
A .NET binary module is referred to as an assembly. Assemblies contain a
combination of IL code and associated metadata. Metadata is a special data
block that stores data type information describing the various objects used in
the assembly, as well as the accurate definition of any object in the program
(including local variables, method parameters, and so on). Assemblies are exe-
cuted by the common language runtime, which loads the metadata into mem-
ory and compiles the IL code into native code using a just-in-time compiler.
Managed Code
Managed code is any code that is verified by the CLR in runtime for security,
type safety, and memory usage. Managed code consists of the two basic .NET
elements: MSIL code and metadata. This combination of MSIL code and meta-
data is what allows the CLR to actually execute managed code. At any given
moment, the CLR is aware of the data types that the program is dealing with.
For example, in conventional compiled languages such as C and C++ data
structures are accessed by loading a pointer into memory and calculating the
specific offset that needs to be accessed. The processor has no idea what this
data structure represents and whether the actual address being accessed is
valid or not.
While running managed code the CLR is fully aware of almost every data
type in the program. The metadata contains information about class defini-
tions, methods and the parameters they receive, and the types of every local
variable in each method. This information allows the CLR to validate opera-
tions performed by the IL code and verify that they are legal. For example,
when an assembly that contains managed code accesses an array item, the
CLR can easily check the size of the array and simply raise an exception if the
index is out of bounds.
426 Chapter 12