12-2

You don't need to fully understand everything you see here. The important thing to remember
is that Program.exe contains a TypeDef whose name is Program. This type identifies a public
sealed class that is derived from System.Object (a type referenced from another assembly).
The Program type also defines two methods: Main and .ctor (a constructor).
I strongly encourage you to experiment with using ILDasm. It can show you a wealth of information,
and the more you understand what you're seeing, the better you'll understand the CLR and
its capabilities. As you'll see, I'll use ILDasm quite a bit more in this book.
Here are some characteristics of assemblies that you should remember:
• An assembly defines the reusable types.
• An assembly is marked with a version number.
• An assembly can have security information associated with it.
I'm sure that many of you reading this are wondering why Microsoft has introduced this new
assembly concept. The reason is that an assembly allows you to decouple the logical and physical
notions of reusable types.
You can create assemblies consisting of types implemented in different programming
languages. For example, you can implement some types in C#, some types in Visual
Basic, and other types in other languages. When you compile the types written with C#
source code, the compiler produces a module. When you compile other types written
with Visual Basic source code, the compiler produces a separate module
. You can then
use a tool to combine all of these modules into a single assembly. To developers using
the assembly, the assembly appears to contain just a bunch of types; developers won't
even know that different programming languages were used. By the way, if you prefer,
you can run ILDasm.exe on each of the modules to obtain an IL source code file. Then
you can run ILAsm.exe and pass it all of the IL source code files. ILAsm.exe will produce
a single file containing all of the types. This technique requires your source code compiler
to produce IL-only code.
。。。。
省略......
真正开始:
Chapter 4
Type Fundamentals
All Types Are Derived from System.Object
system.object基类定义了以下public的实例方法:
Equal
GetHashCode
ToString
GetType
Employee e = new Employee("ConstructorParam1");
Here's what the new operator does:
.1. It calculates the number of bytes required by all instance fields defined in the type and
all of its base types up to and including System.Object (which defines no instance fields
of its own). Every object on the heap requires some additional members—called the type
object pointer and the sync block index—used by the CLR to manage the object. The
bytes for these additional members are added to the size of the object.
2. It allocates memory for the object by allocating the number of bytes required for the
specified type from the managed heap; all of these bytes are then set to zero (0).
3. It initializes the object's type object pointer and sync block index members.
4. The type's instance constructor is called, passing it any arguments (the string
"ConstructorParam1" in the preceding example) specified in the call to new. Most compilers
automatically emit code in a constructor to call a base class's constructor. Each
constructor is responsible for initializing the instance fields defined by the type whose
constructor is being called. Eventually, System.Object's constructor is called, and this
constructor method does nothing but return. You can verify this by using ILDasm.exe to
load MSCorLib.dll and examine System.Object's constructor method.
创建如此创建,回收却是GC管。
 Casting Between Types(类型转换)
 To make sure you understand everything just presented, take the following quiz. Assume that
these two class definitions exist:
internal class B { // Base class
}
internal class D : B { // Derived class
}
小测验,其中的几个强制转换需要理解,向上转换。注意这种 B b = new D();(其中D的基类是B)在此需注意的是,创建的对象是D类型,但是b指针是B类型的指针,所以当b要转换成D类型时,要强制转换,用(D)b,要记住。
Namespaces and Assemblies
namespace是可以解决相通命名冲突的。
How Things Relate at Run Time
.....
Chapter 5
Primitive, Reference, and
Value Types
Any data types the compiler directly supports are called primitive types.
int a = 0; // Most convenient syntax
System.Int32 a = 0; // Convenient syntax
int a = new int(); // Inconvenient syntax
System.Int32 a = new System.Int32(); // Most inconvenient syntax
Checked and Unchecked Primitive Type Operations
讲述溢出的。
Here's the best way to go about using checked and unchecked:
• As you write your code, explicitly use checked around blocks where an unwanted overflow
might occur due to invalid input data, such as processing a request with data supplied
from an end user or a client machine.
• As you write your code, explicitly use unchecked around blocks where an overflow is
OK, such as calculating a checksum.
• For any code that doesn't use checked or unchecked, the assumption is that you do want
an exception to occur on overflow, for example, calculating something (such as prime
numbers) where the inputs are known, and overflows are bugs.
Reference Types and Value Types
Value type instances are usually allocated on a thread's stack
 
posted @ 2009-12-02 13:08  Tmac_  阅读(170)  评论(0编辑  收藏  举报