ch03_The .NET Type System.pdf

(220 KB) Pobierz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Chapter 3
The .NET Type System
This chapter provides a tour through the .NET types needed for Compact Framework
programming. [Comment 3.1]
Author's Note: In this review chapter C#-Specific Text is highlighted in YELLOW.
Numeric Conversion ......................................................................................................... 21
Explicit Conversion .............................................................................................. 21
The Overflow Exception ...................................................................................... 22
Handling Exceptions............................................................................................ 23
Converting Strings to Numbers ........................................................................... 23
String Conversion ............................................................................................................. 24
Numeric Formatting ............................................................................................. 24
Character Set Conversion ................................................................................... 25
Creating Formatted Strings ................................................................................. 25
Converting Values to Objects ........................................................................................... 26
Memory Management.................................................................................................................... 27
Metadata Maps ................................................................................................................. 27
The Jitted Code Pool ........................................................................................................ 28
Garbage Collector Pools .................................................................................................. 29
Garbage Collection and Data ........................................................................................... 30
Automatic Garbage Collection.......................................................................................... 31
Phase 1: Mark and Sweep .................................................................................. 31
Phase 2: Compact ............................................................................................... 32
Phase 3: Flush Jitted Code Pool ......................................................................... 32
Chapter 3 – Data Types
Page 1
Copyright © 2003 Paul Yao & David Durant
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
Special Handling for Managed Data................................................................................. 32
Calling the Garbage Collector ............................................................................. 32
Manual Cleanup using Dispose ........................................................................ 33
Implementing Class-Specific Cleanup Methods: Dispose and Finalize ....... 33
Weak References ................................................................................................ 37
Manual Garbage Collection for Native Data..................................................................... 37
Conclusion ..................................................................................................................................... 42
.NET programming provides a high level of interoperability between programming
languages. Interoperability is achieved by a set of common standards. Among the .NET
technologies that Microsoft submitted to ECMA 1 , a European standards body, was the Common
Language Infrastructure (CLI) . The CLI contains basic infrastructure elements of .NET, but
excludes the high-level Windows Forms and Web Forms classes. To interoperate with other
.NET-compatible languages, a compiler must comply with the CLI. This chapter focuses on one
of those standards: the Common Type System (CTS). [Comment 3.3]
The .NET type system provides the foundation for a strongly-typed, object-oriented
programming environment. Strong-typing codifies a programmer's intent for how each data item
by choosing the most appropriate type. Once the type is chosen, errors can be detected at both
build-time (by the compiler) and at runtime (by the runtime system). The .NET execution engine
takes this one step further, supporting verifiable code which relies on strong typing to insure that
the code does not access out-of-bounds memory. [Comment 3.4]
An object-oriented approach organizes the raw material of software – meaning the code that
runs and the data that is operated on – as objects . The content and behavior of an object is
defined, in turn, by its type – that is, by its class . Programmers use classes to organize code and
data. Using the terminology of .NET, we say that a class is a set of methods and fields , where a
method is the most fundamental type of code, and a field is the most fundamental type of data.
This chapter reviews the elements of the .NET framework type system that Compact
Framework programmers are likely to need. Our intent is not to teach C# programming – there
are other books better suited for that purpose. Nor is our intent to provide a complete discussion
of the intricacies of .NET internals, a fascinating subject which is addressed in other, more
specialized, books 2 . [Comment 3.6]
Because this book focuses on C#, we expect readers to have learned C# programming from
other sources. For readers with a background in C++ and object-oriented theory, we provide
some boxed discussions labeled "For C++ Programmers." For readers with a background in VB6
programming, we provide some boxed discussions labeled "For VB6 Programmers." [Comment
Namespaces
To help organize the large number of types that are inevitable in a large, multi-faceted
programming environment, C# programmers use namespaces . A namespace provides part of an
object's identity; it is the family name for a set of types. For example, the System.Drawing
namespace is the family of types used to create graphical output. Within that namespace are two
sibling classes, Brush and Pen . The fully-qualified names for these types are
1 ECMA web site: www.ecma-international.org
2 We recommend Essential .NET: The Common Language Runtime by Don Box with Chris Sells,
Addison-Wesley, 2003.
Chapter 3 – Data Types
Page 2
Copyright © 2003 Paul Yao & David Durant
870585282.016.png 870585282.017.png 870585282.018.png 870585282.001.png
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
System.Drawing.Brush and System.Drawing.Pen . The fully-qualified name gives these
types a unique identity among the vast set of .NET-compatible types. [Comment 3.8]
Every .NET program and library file contains metadata , a dictionary of the types defined in a
module 3 . At build time, a compiler creates metadata, which the execution engine uses at runtime.
Objects are instantiated using type information, and code verified for compatibility with the
supported types. Every .NET type is defined in an executable module file as metadata. [Comment
Namespaces, Classes, Methods, and Fields
Namespaces are optional, but we like using them to help organize our code. For class
libraries, the convention is that the name of a namespace should also be the name of the DLL file.
For example, in the Compact Framework, the System.Windows.Forms namespace resides
inside the file System.Windows.Forms.dll . Microsoft suggests combining these elements for
namespace names in public libraries: company name, technology name, feature, and design. In
the examples for this book, we use the YaoDurant namespace for shared libraries, or for source
files explicitly built for reuse. [Comment 3.10]
In .NET, everything is a value or an object. As a result, stand-alone data – what C++
programmers and VB programmers call global variables – does not exist. Stand-alone global
functions also do not exist. Instead, both code and data must live within a class. [Comment 3.11]
The two fundamental .NET types are fields and methods . The tiny fragment in listing 3-1
shows a field and a method defined in a class. The class, in turn, lives in a namespace,
YaoDurant.Identity . [Comment 3.12]
Listing 3-1 Namespaces contain classes, which contain fields and methods [Comment
namespace YaoDurant.Identity
{
public class SimpleType
{
public int i;
public int ReturnInteger()
{
return i;
}
} // class
} // namespace
This code contains examples of four key elements of the .NET type system: [Comment 3.14]
A namespace – YaoDu rant.Identity [Comment 3.15]
A class - SimpleType [Com ment 3.16]
A field - i [Comment 3.17]
A method - ReturnInteger [Comment 3.18]
3 By module , we mean an application (.exe) file or a dynamic link library (.dll) file. Some
programming environments use the term to refer to a source file.
Chapter 3 – Data Types
Page 3
Copyright © 2003 Paul Yao & David Durant
870585282.002.png 870585282.003.png 870585282.004.png 870585282.005.png 870585282.006.png
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
The fully-qualified name for the class is YaoDurant.Identity.SimpleType , and the fully-
qualified name for the method is YaoDurant.Identity.SimpleType.ReturnInteger . We
discuss the using statement in the next section, which lets you reference a namespace to avoid
having to use fully-qualified names. [Comment 3.19]
A program source file can have any number of namespaces. The only thing which can reside
in a namespace is another namespace, or a class. The purpose of namespaces is to help organize
classes, which are the general-purpose container for holding type information. A class is able to
contain all other types, including other classes. (A namespace is not a type, and so cannot be
contained in a class.) [Comment 3.20]
Accessing Namespaces
A fully-qualified name provides an unambiguous way to identify a class, methods in a class,
and static (or shared) fields. That is a good thing. When you look at the code generated for you by
the forms designer, you see a lot of fully-qualified names. The forms designer writes the code for
you. It later reads the code when you decide to edit a form. In this context, a fully-qualified name
helps the forms designer avoid confusion. [Comment 3.21]
The disadvantage of fully-qualified names is that they require a lot of typing. To avoid this
extra effort, C# programmers indicate the namespaces they wish to use with a set of using
statements. For example, if we had a separate source file, module, or assembly which needed to
access the types in our namespace, we would eliminate the need for fully-qualified names with a
statement like the following:
using YaoDurant.Identity;
Assembly versus Namespace
Close reading of the CLI reveals that an assembly , not a namespace, forms
the outermost container of the .NET type system. One reason is that
some CLI-compatible languages might not support namespaces. For
most purposes, a namespace serves as the outermost container for
everything in the .NET type system. There are times, however, when a
namespace falls short of providing what we need. [Comment 3.23]
A program sometimes needs to identify itself to the execution engine.
There is no namespace class; instead, we use an object created from the
Assembly 4 class. We use an assembly object to access resources in a
module, to load and run programs and shared libraries, and also to query
for type information. [Comment 3.24]
By itself, a using statement hints at desired namespaces, but does not
indicate what actual module to use. For that, you must add a reference to
a Visual Studio .NET project, a subject we discuss in the accompanying
On the desktop, an assembly can be created from two or more modules.
The Compact Framework does not support multi-module assembly, so
every assembly has exactly one module associated with it. [Comment 3.26]
4 Fully-qualified name: System.Reflection.Assembly .
Chapter 3 – Data Types
Page 4
Copyright © 2003 Paul Yao & David Durant
870585282.007.png 870585282.008.png 870585282.009.png 870585282.010.png 870585282.011.png 870585282.012.png 870585282.013.png 870585282.014.png
 
Programming the .NET Compact Framework in C#
By Paul Yao & David Durant
When you create a new Smart Device project in Visual Studio .NET, the Smart Device
Application Wizard creates a set of using statements for you, as shown in figure 3-1. These are
not needed to build the code that has been generated for you, because the forms designer uses
fully-qualified names. Instead, these references tell the IDE about the namespaces needed to
support IntelliSense.
Figure 3-1 Using declarations created by the Smart Device Application Wizard
Referencing Assemblies
An assembly reference provides the names of the shared libraries that a .NET program (or
shared library) is going to use. Unlike a namespace, which is a logical container for a set of
classes, an assembly consists of one or more module files which are the physical container for a
set of classes. Any using statements provide a hint about what namespaces might be interesting,
but those statements are purely optional. The assembly reference, on the other hand, is not
When the Smart Device Application Wizard creates a project for you, it adds a set of
assembly references to your project. Figure 3-2 shows an example of the default set that are
provided for you in the solution explorer window. You add new assembly references by
summoning the Add Reference dialog box (from the Visual Studio menu, select Project -> Add
Reference... ; or Right-Click on the Solution Explorer and select Add Reference... ). [Comment
Chapter 3 – Data Types
Page 5
Copyright © 2003 Paul Yao & David Durant
870585282.015.png
Zgłoś jeśli naruszono regulamin