Tải bản đầy đủ (.pdf) (7 trang)

a0068 c sharp 4 0 msp clr via c 3rd edition 201 morebook vn 3434

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (9.29 MB, 7 trang )


PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2010 by Jeffrey Richter
All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means
without the written permission of the publisher.
Library of Congress Control Number: 2009943026
Printed and bound in the United States of America.
1 2 3 4 5 6 7 8 9 WCT 5 4 3 2 1 0
A CIP catalogue record for this book is available from the British Library.
Microsoft Press books are available through booksellers and distributors worldwide. For further infor­mation about
international editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly at
fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress. Send comments to
Microsoft, Microsoft Press, Active Accessibility, Active Directory, ActiveX, Authenticode, DirectX, Excel, IntelliSense,
Internet Explorer, MSDN, Outlook, SideShow, Silverlight, SQL Server, Visual Basic, Visual Studio, Win32, Windows,
Windows Live, Windows Media, Windows NT, Windows Server and Windows Vista are either registered trademarks
or trademarks of the Microsoft group of companies. Other product and company names mentioned herein may be the
trademarks of their respective owners.
The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events
depicted herein are fictitious. No association with any real company, organization, product, domain name, e-mail address,
logo, person, place, or event is intended or should be inferred.
This book expresses the author’s views and opinions. The information contained in this book is provided without any
express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributors will
be held liable for any damages caused or alleged to be caused either directly or indirectly by this book.
Acquisitions Editor: Ben Ryan
Developmental Editor: Devon Musgrave
Project Editor: Valerie Woolley
Editorial Production: Custom Editorial Productions, Inc.


Technical Reviewer: Christophe Nasarre; Technical Review services provided by Content Master, a member of CM
Group, Ltd.
Cover: Tom Draper Design
Body Part No. X16-61995


Table of Contents
Foreward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv

Part I CLR Basics



1 The CLR’s Execution Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Compiling Source Code into Managed Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Combining Managed Modules into Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Loading the Common Language Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Executing Your Assembly’s Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
IL and Verification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Unsafe Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
The Native Code Generator Tool: NGen.exe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
The Framework Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
The Common Type System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
The Common Language Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Interoperability with Unmanaged Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29



2 Building, Packaging, Deploying, and Administering Applications


and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
.NET Framework Deployment Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Building Types into a Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Response Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
A Brief Look at Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Combining Modules to Form an Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Adding Assemblies to a Project by Using the Visual Studio IDE . . . . . . . . . . . . . . 49
Using the Assembly Linker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Adding Resource Files to an Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Assembly Version Resource Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Version Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Culture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Simple Application Deployment (Privately Deployed Assemblies) . . . . . . . . . . . . . . . . . . 59
Simple Administrative Control (Configuration) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

What do you think of this book? We want to hear from you!
Microsoft is interested in hearing your feedback so we can continually improve our books and learning
resources for you. To participate in a brief online survey, please visit:

www.microsoft.com/learning/booksurvey/



iii


iv

Table of Contents




3 Shared Assemblies and Strongly Named Assemblies . . . . . . . . . . . . . . 65
Two Kinds of Assemblies, Two Kinds of Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Giving an Assembly a Strong Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Global Assembly Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Building an Assembly That References a Strongly Named Assembly . . . . . . . . . . . . . . .
Strongly Named Assemblies Are Tamper-Resistant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Delayed Signing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Privately Deploying Strongly Named Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How the Runtime Resolves Type References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Advanced Administrative Control (Configuration) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Publisher Policy Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

66
67
73
75
76
77
80
81
84
87

Part II Designing Types




4 Type Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
All Types Are Derived from System.Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Casting Between Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Casting with the C# is and as Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Namespaces and Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
How Things Relate at Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102



5 Primitive, Reference, and Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Programming Language Primitive Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Checked and Unchecked Primitive Type Operations . . . . . . . . . . . . . . . . . . . . . . .
Reference Types and Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Boxing and Unboxing Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Changing Fields in a Boxed Value Type by Using Interfaces (and Why You
Shouldn’t Do This) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Object Equality and Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Object Hash Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The dynamic Primitive Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .



113
117
121
127
140
143
146
148


6 Type and Member Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
The Different Kinds of Type Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Type Visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Friend Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Member Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Static Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Partial Classes, Structures, and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Components, Polymorphism, and Versioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
How the CLR Calls Virtual Methods, Properties, and Events . . . . . . . . . . . . . . . . 167
Using Type Visibility and Member Accessibility Intelligently . . . . . . . . . . . . . . . . 172
Dealing with Virtual Methods When Versioning Types . . . . . . . . . . . . . . . . . . . . . 175



7 Constants and Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181
Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183




Table of Contents



8 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Instance Constructors and Classes (Reference Types) . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Instance Constructors and Structures (Value Types) . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Type Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Type Constructor Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Operator Overload Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Operators and Programming Language Interoperability . . . . . . . . . . . . . . . . . . .
Conversion Operator Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Extension Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Rules and Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Extending Various Types with Extension Methods . . . . . . . . . . . . . . . . . . . . . . . .
The Extension Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Partial Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Rules and Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .



187
191
194
198
200
203
204
207
210
211
213
213
216

9 Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Optional and Named Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Rules and Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

The DefaultParameterValue and Optional Attributes . . . . . . . . . . . . . . . . .
Implicitly Typed Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Passing Parameters by Reference to a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Passing a Variable Number of Arguments to a Method . . . . . . . . . . . . . . . . . . . . . . . . . .
Parameter and Return Type Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Const-ness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

219
220
222
223
225
231
233
235

10 Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Parameterless Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Automatically Implemented Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining Properties Intelligently . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Object and Collection Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Anonymous Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The System.Tuple Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Parameterful Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Performance of Calling Property Accessor Methods . . . . . . . . . . . . . . . . . . . . . . . .
Property Accessor Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generic Property Accessor Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

237
241

242
245
247
250
252
257
258
258

11 Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Designing a Type That Exposes an Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step #1: Define a type that will hold any additional information
that should be sent to receivers of the event notification . . . . . . . . . . . . . . . .
Step #2: Define the event member . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Step #3: Define a method responsible for raising the event to
notify registered objects that the event has occurred . . . . . . . . . . . . . . . . . . . .
Step #4: Define a method that translates the input into the desired event . . .
How the Compiler Implements an Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

260
261
262
263
266
266

v


If you like the book you’ll love the training

WINTELLECT TRAINING COURSES BY JEFFREY RICHTER
Effective Threading in C#: Mastering Responsive, Reliable and Scalable Applications
Duration and Format: 2 Day On-Site/ Virtual
Syllabus

Day 1

• Introduction, CPU industry trends
• Thread creation/destruction, overhead, scheduling and priorities
• Tools for monitoring and debugging threads
• Reasons to use threads and why
• Performing asynchronous compute-bound operations using the CLR’s thread pool, Timers,
and Tasks (new in .NET 4.0)
• Performing asynchronous I/O-bound operations using the CLR’s Asynchronous Programming
Model
• Using special language features (anonymous methods, lambda expressions, and iterators)
to make asynchronous programming easier.

Day 2

• Performing asynchronous I/O-bound operations using the Event-based Asynchronous
Programming Model
• Primitive (user-mode and kernel-mode) thread synchronization constructs including
volatile fields, interlocked methods, SpinLocks
• Hybrid thread synchronization constructs including mutual exclusive locks, reader-writer
locks, new .NET 4.0 locks
• Comparing the behavior and performance of locks
• How locks work internally and how to modify a lock’s behavior
• The ReaderWriterGate: A lock that doesn’t block any threads.


Mastering the .NET Framework
Duration and Format: 5 Day On-Site/ 3 Day On-Site/ Virtual
Syllabus
Day 1

• Introduction to the .NET Framework, Motivating its Use, and its Core Technologies
• The .NET Framework’s Development Platform’s Architecture
• Building, Deploying, Versioning, and Administering Applications and Components

Day 2

• Type Fundamentals (Type-safety, Value and Reference types, boxing)
• Type Members
• Essential Types

Day 3

• Working with Text (characters, strings, encodings, cultures, formatting, parsing)
• Generics (types, methods, interfaces, verifiability and constraints, collections)
• Exception Handling and State Management

Day 4

• Automatic Memory Management
• Language Enhancements (Iterators, LINQ , Dynamic)

Day 5

• Streams and Serialization (Stream composability, Binary, Soap, and XML serialization)
• Building Dynamically-Extensible Applications (AppDomains, Assemblies, Reflection)

• Interoperating with Unmanaged Code (COM and P/Invoke)
To learn more about Wintellect training offerings visit www.wintellect.com.


What do
you think of
this book?
We want to hear from you!
To participate in a brief online survey, please visit:

microsoft.com/learning/booksurvey

Tell us how well this book meets your needs­—what works effectively, and what we can
do better. Your feedback will help us continually improve our books and learning
resources for you.
Thank you in advance for your input!

Stay in touch!
To subscribe to the Microsoft Press® Book Connection Newsletter—for news on upcoming
books, events, and special offers—please visit:
microsoft.com/learning/books/newsletter



×