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

Essential C# 3.0 FOR NET FRAMEWORK 3.5 PHẦN 1 docx

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 (5.66 MB, 88 trang )

Praise for Essential C# 3.0
“In a time when you can search online for any reference material, Essential C# 3.0
is the one book I still bother to carry in my bag. Any developer can hit F1 for
help with the language syntax; this book, however, arms me with the knowl-
edge to make better design and coding decisions (and the confidence to say so).
You know when a book has more than twenty pages of index that it’s got depth
covered, and although Essential C# 3.0 has introductory chapters, it progresses
effortlessly into the lesser known corners of the language, which makes this
book indispensible to any level of C# developer.”
—Troy Magennis, C# MVP and creator of HookedOnLINQ.com
“If you are new to C#, as I was, Mark’s book is an excellent way to start. His
approach takes you step by step through the structure and syntax of the lan-
guage, making it easy to understand how things work. I found the code sam-
ples extremely helpful and supportive of the concepts being discussed.”
—Robert Stokesbary, senior consultant, Option Explicit
“This book expands on the concepts introduced in Essential C# 2.0 to include C#
3.0 enhancements. One great aspect of this book is that it not only covers the
new 3.0 enhancements in great detail, but it also shows where it makes sense to
utilize these new features over the existing 2.0 features. While I have been pro-
gramming C# for many years, I find myself referring to this book often and
always seem to find something new buried in the pages. This is a must-read for
both the C# newbie as well as the C# seasoned veteran.”
—Michael Stokesbary, senior software engineer, Itron Inc.
Praise for the First Edition
“Essential C# 2.0 pulls off a very difficult task. The early chapters are comprehen-
sible by beginning developers, while the later chapters pull no punches and
provide the experienced developer with the detailed information they need to
make the most of C# 2.0. Starting with the first chapter, Mark has successfully
interwoven tidbits of information useful to even the most advanced developer
while keeping the book approachable.”


—Chris Kinsman, chief architect, Vertafore, Microsoft Regional Director
“How refreshing! This book deals with C# thoroughly, rather than skimming over
the whole .NET framework. It is valuable to newcomers and professionals alike.”
—Jon Skeet, C# MVP
“Essential C# 2.0 is a one-stop shop for an experienced programmer looking to
ramp up on one of the hottest languages around today. Mark delivers an intelli-
gent and detailed tour of C#, providing newcomers to the language with a solid
foundation of skill on which to build their next generation of applications.”
—Stephen Toub, technical editor, MSDN Magazine
“This book provides complete, up-to-date coverage of all the programming con-
structs in C#. Masterfully organized, it allows beginning programmers to get
on board and leads more experienced programmers into the world of struc-
tured programming. Because of its unwavering focus on the essential program-
ming constructs of C#—such as generics, delegates, and much more—this book
is indispensable. For programmers who want to solve their day-to-day pro-
gramming issues using the latest features this modern programming language
has to offer, this book is indispensable.”
—Narendra Poflee, IT integration specialist, Itron Inc.
“Essential C# 2.0 is an ideal book for all programmers interested in C#. If you are
a beginner, you will quickly learn the basics of C# programming and become
familiar with the concepts. The flow of the text is easy to follow and does a
great job of not repeating concepts that have already been covered. For the
experienced programmer, this book has priceless nuggets embedded within its
pages, making it a great read for programmers who are already familiar with
C#. This will be a book that I will keep next to my computer for years to come.”
—Michael Stokesbary, software engineer, Itron Inc.
Essential C# 3.0
Microsoft .NET Development Series
John Montgomery, Series Advisor
Don Box, Series Advisor

Brad Abrams, Series Advisor
The award-winning Microsoft .NET Development Series was established in 2002 to provide professional
developers with the most comprehensive and practical coverage of the latest .NET technologies. It is
supported and developed by the leaders and experts of Microsoft development technologies, including
Microsoft architects, MVPs, and leading industry luminaries. Books in this series provide a core resource of
information and understanding every developer needs to write effective applications.
Titles in the Series
Brad Abrams, .NET Framework Standard Library
Annotated Reference Volume 1: Base Class Library and
Extended Numerics Library, 978-0-321-15489-7
Brad Abrams and Tamara Abrams, .NET Framework
Standard Library Annotated Reference, Volume 2:
Networking Library, Reflection Library, and XML Library,
978-0-321-19445-9
Chris Anderson, Essential Windows Presentation Foundation
(WPF), 978-0-321-37447-9
Bob Beauchemin and Dan Sullivan, A Developer’s Guide to
SQL Server 2005, 978-0-321-38218-4
Adam Calderon, Joel Rumerman, Advanced ASP.NET
AJAX Server Controls: For .NET Framework 3.5,
978-0-321-51444-8
Eric Carter and Eric Lippert, Visual Studio Tools for Office:
Using C# with Excel, Word, Outlook, and InfoPath,
978-0-321-33488-6
Eric Carter and Eric Lippert, Visual Studio Tools for
Office: Using Visual Basic 2005 with Excel, Word, Outlook,
and InfoPath, 978-0-321-41175-4
Steve Cook, Gareth Jones, Stuart Kent, Alan Cameron
Wills, Domain-Specific Development with Visual Studio
DSL Tools, 978-0-321-39820-8

Krzysztof Cwalina and Brad Abrams, Framework Design
Guidelines: Conventions, Idioms, and Patterns for Reusable
.NET Libraries, Second Edition, 978-0-321-54561-9
Joe Duffy, Concurrent Programming on Windows,
978-0-321-43482-1
Sam Guckenheimer and Juan J. Perez, Software
Engineering with Microsoft Visual Studio Team System,
978-0-321-27872-2
Anders Hejlsberg, Mads Torgersen, Scott Wiltamuth,
Peter Golde, The C# Programming Language, Third Edition,
978-0-321-56299-9
Alex Homer and Dave Sussman, ASP.NET 2.0 Illustrated,
978-0-321-41834-0
Joe Kaplan and Ryan Dunn, The .NET Developer’s Guide to
Directory Services Programming, 978-0-321-35017-6
Mark Michaelis, Essential C# 3.0: For .NET Framework 3.5,
978-0-321-53392-0
James S. Miller and Susann Ragsdale,
The Common Language Infrastructure Annotated Standard,
978-0-321-15493-4
Christian Nagel, Enterprise Services with the .NET
Framework: Developing Distributed Business Solutions
with .NET Enterprise Services, 978-0-321-24673-8
Brian Noyes, Data Binding with Windows Forms 2.0:
Programming Smart Client Data Applications with .NET,
978-0-321-26892-1
Brian Noyes, Smart Client Deployment with ClickOnce:
Deploying Windows Forms Applications with ClickOnce,
978-0-321-19769-6
Fritz Onion with Keith Brown, Essential ASP.NET 2.0,

978-0-321-23770-5
Steve Resnick, Richard Crane, Chris Bowen, Essential
Windows Communication Foundation: For .NET Framework
3.5, 978-0-321-44006-8
Scott Roberts and Hagen Green, Designing Forms
for Microsoft Office InfoPath and Forms Services 2007,
978-0-321-41059-7
Neil Roodyn, eXtreme .NET: Introducing eXtreme
Programming Techniques to .NET Developers,
978-0-321-30363-9
Chris Sells and Michael Weinhardt,
Windows Forms 2.0
Programming, 978-0-321-26796-2
Dharma Shukla and Bob Schmidt, Essential Windows
Workflow Foundation, 978-0-321-39983-0
Guy Smith-Ferrier, .NET Internationalization:
The Developer’s Guide to Building Global Windows
and Web Applications, 978-0-321-34138-9
Will Stott and James Newkirk, Visual Studio Team
System: Better Software Development for Agile Teams,
978-0-321-41850-0
Paul Yao and David Durant, .NET Compact Framework
Programming with C#, 978-0-321-17403-1
Paul Yao and David Durant, .NET Compact Framework
Programming with Visual Basic .NET, 978-0-321-17404-8
For more information go to informit.com/msdotnetseries/
Upper Saddle River, NJ • Boston • Indianapolis • San Francisco
New York • Toronto • Montreal • London • Munich • Paris • Madrid
Capetown • Sydney • Tokyo • Singapore • Mexico City
Essential

C# 3.0
For .NET Framework 3.5
Mark Michaelis
Many of the designations used by manufacturers and sellers to distinguish their products are claimed
as trademarks. Where those designations appear in this book, and the publisher was aware of a trade-
mark claim, the designations have been printed with initial capital letters or in all capitals.
The .NET logo is either a registered trademark or a trademark of Microsoft Corporation in the United
States and/or other countries and is used under license from Microsoft.
The author and publisher have taken care in the preparation of this book, but make no expressed or
implied warranty of any kind and assume no responsibility for errors or omissions. No liability is
assumed for incidental or consequential damages in connection with or arising out of the use of the
information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or
special sales, which may include electronic versions and/or custom covers and content particular to
your business, training goals, marketing focus, and branding interests. For more information, please
contact:
U.S. Corporate and Government Sales
(800) 382-3419

For sales outside the United States please contact:
International Sales

Visit us on the Web: informit.com/aw
Library of Congress Cataloging-in-Publication Data
Michaelis, Mark.
Essential C# 3.0 : for .NET Framework 3.5 / Mark Michaelis.
p. cm.
Includes index.
ISBN 978-0-321-53392-0 (pbk. : alk. paper) 1. C# (Computer program language)
2. Microsoft .NET Framework. I. Title.

QA76.73.C154M5235 2008
006.7’882—dc22
2008023595
Copyright © 2009 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright,
and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a
retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying,
recording, or likewise. For information regarding permissions, write to:
Pearson Education, Inc
Rights and Contracts Department
501 Boylston Street, Suite 900
Boston, MA 02116
Fax (617) 671 3447
ISBN-13: 978-0-321-53392-0
ISBN-10: 0-321-53392-5
Text printed in the United States on recycled paper at Courier in Stoughton, Massachusetts.
First printing, August 2008
To my family: Elisabeth, Benjamin, Hanna, and Abigail.
This page intentionally left blank
ix
Contents at a Glance
Contents xi
Figures xxiii
Ta ble s xx v
Foreword xxvii
Preface xxxi
Acknowledgments xliii
About the Author xlv
1 Introducing C# 1
2 Data Types 31

3 Operators and Control Flow 83
4 Methods and Parameters 147
5 Classes 195
6 Inheritance 261
7 Interfaces 295
8 Value Types 321
9 Well-Formed Types 347
10 Exception Handling 389
11 Generics 405
12 Delegates and Lambda Expressions 445
Contents at a Glancex
13 Events 479
14 Collection Interfaces with Standard Query Operators 507
15 Query Expressions 555
16 Building Custom Collections 577
17 Reflection and Attributes 617
18 Multithreading 657
19 Multithreading Patterns 699
20 Platform Interoperability and Unsafe Code 723
21 The Common Language Infrastructure 749
A Downloading and Installing the C# Compiler and the
CLI Platform 771
B Full Source Code Listings 775
C C# 3.0 Topics 801
Index 807
xi
Contents
Figures xxiii
Ta ble s xx v
Foreword xxvii

Preface xxxi
Acknowledgments xliii
About the Author xlv
1 Introducing C# 1
Hello, World 2
Compiling and Running the Application 3
C# Syntax Fundamentals 4
C# Keywords 5
Type Definition 6
Main 7
Whitespace 10
Working with Variables 12
Data Types 12
Declaring a Variable 13
Assigning a Variable 14
Using a Variable 15
Console Input and Output 15
Getting Input from the Console 16
Writing Output to the Console 17
Comments 19
Managed Execution and the Common Language Infrastructure 22
Contents
Contentsxii
C# and .NET Versioning 25
Common Intermediate Language and ILDASM 26
2 Data Types 31
Fundamental Numeric Types 32
Integer Types 32
Floating-Point Types (
float, double)33

Decimal Type 34
Literal Values 35
More Fundamental Types 40
Boolean Type (bool)40
Character Type (char)41
Strings 43
null and void 51
null 51
The void Nontype 52
Categories of Types 55
Value Types 55
Reference Types 56
Nullable Modifier 57
Conversions between Data Types 58
Explicit Cast 59
Implicit Cast 62
Type Conversion without Casting 62
Arrays 65
Declaring an Array 65
Instantiating and Assigning Arrays 66
Using an Array 70
Strings as Arrays 76
Common Errors 78
3 Operators and Control Flow 83
Operators 84
Plus and Minus Unary Operators (+, –)84
Arithmetic Binary Operators (+, –, *, /, %)85
Parenthesis Operator 92
Assignment Operators (+=, –=, *=, /=, %=)93
Increment and Decrement Operators (++, ––)94

Constant Expressions (const)98
Introducing Flow Control 98
if Statement 102
Nested if 103
Contents xiii
Code Blocks ({})105
Scope 107
Boolean Expressions 108
Relational and Equality Operators 109
Logical Boolean Operators 111
Logical Negation Operator (!)112
Conditional Operator (?)113
Bitwise Operators (<<, >>, |, &, ^, ~)114
Shift Operators (<<, >>, <<=, >>=)115
Bitwise Operators (&, |, ^)116
Bitwise Assignment Operators (&=, |=, ^=)118
Bitwise Complement Operator (~)119
Control Flow Statements, Continued 119
The while and do/while Loops 119
The for loop 122
The foreach Loop 125
The switch Statement 128
Jump Statements 130
The break Statement 131
The continue Statement 133
The goto Statement 135
C# Preprocessor Directives 136
Excluding and Including Code (#if, #elif, #else, #endif) 138
Defining Preprocessor Symbols (#define, #undef) 139
Emitting Errors and Warnings (#error, #warning) 139

Turning Off Warning Messages (#pragma)140
nowarn:<warn list> Option 141
Specifying Line Numbers (#line) 141
Hints for Visual Editors (#region, #endregion) 142
4 Methods and Parameters 147
Calling a Method 148
Namespace 150
Type Name 152
Scope 153
Method Name 153
Parameters 153
Method Return 153
Statement versus Method Call 154
Declaring a Method 155
Parameter Declaration 157
Method Return Declaration 157
Contentsxiv
The using Directive 160
Aliasing 163
Returns and Parameters on Main() 164
Parameters 167
Value Parameters 167
Reference Parameters (ref)168
Output Parameters (out) 170
Parameter Arrays (params)172
Recursion 174
Method Overloading 177
Basic Error Handling with Exceptions 180
Trapping Errors 181
Reporting Errors Using a throw Statement 189

5 Classes 195
Defining and Instantiating a Class 199
Instance Fields 203
Declaring an Instance Field 203
Accessing an Instance Field 204
Instance Methods 205
Using the this Keyword 206
Access Modifiers 213
Properties 215
Declaring a Property 217
Automatically Implemented Properties 219
Naming Conventions 220
Using Properties with Validation 221
Read-Only and Write-Only Properties 223
Access Modifiers on Getters and Setters 224
Properties as Virtual Fields 225
Properties and Method Calls Not Allowed as ref or out Parameter Values 227
Constructors 229
Declaring a Constructor 229
Default Constructors 231
Object Initializers 232
Overloading Constructors 234
Calling Another Constructor Using this 235
Static 239
Static Fields 240
Static Methods 243
Static Constructors 245
Static Properties 246
Static Classes 247
Contents xv

Extension Methods 249
Encapsulating the Data 250
const 251
readonly 251
Nested Classes 252
Partial Classes 254
Defining a Partial Class 255
Partial Methods 256
6 Inheritance 261
Derivation 262
Casting between Base and Derived Types 264
private Access Modifier 267
protected Access Modifier 268
Extension Methods 269
Single Inheritance 269
Sealed Classes 272
Overriding the Base Class 272
virtual Modifier 273
new Modifier 278
sealed Modifier 282
base Member 282
Constructors 283
Abstract Classes 284
Everything Ultimately Derives from System.Object 290
Verifying the Underlying Type with the is Operator 292
Conversion Using the as Operator 293
7 Interfaces 295
Introducing Interfaces 296
Polymorphism through Interfaces 297
Interface Implementation 302

Explicit Member Implementation 304
Implicit Member Implementation 305
Explicit versus Implicit Interface Implementation 306
Casting between the Implementing Class and Its Interfaces 307
Interface Inheritance 308
Multiple Interface Inheritance 310
Extension Methods on Interfaces 311
Implementing Multiple Inheritance via Interfaces 313
Versioning 315
Interfaces Compared with Classes 317
Contentsxvi
8 Value Types 321
Structs 322
Initializing structs326
Using the default Operator 328
Inheritance and Interfaces with Value Types 328
Boxing 329
Enums 335
Type Compatibility between Enums 339
Converting between Enums and Strings 339
Enums as Flags 340
9 Well-Formed Types 347
Overriding object Members 347
Overriding ToString() 348
Overriding GetHashCode() 348
Overriding Equals() 350
Guidelines for Implementing Equality 357
Operator Overloading 358
Comparison Operators (==, !=, <, >, <=, >=) 358
Binary Operators (+, –, *, /, %, &, |, ^, <<, >>)359

Combining Assignment with Binary Operators (+=, –=, *=, /=, %=, &=…) 361
Conditional Logical Operators (&&,
||) 361
Unary Operators (+, –, !, ~, ++, ––, true, false) 362
Conversion Operators 363
Guidelines for Conversion Operators 365
Referencing Other Assemblies 365
Changing the Assembly Target 366
Referencing an Assembly 367
Encapsulation of Types 368
Defining Namespaces 370
Namespace Alias Qualifier 372
XML Comments 373
Associating XML Comments with Programming Constructs 374
Generating an XML Documentation File 376
Garbage Collection 377
Weak References 379
Resource Cleanup 381
Finalizers 381
Deterministic Finalization with the using Statement 382
Garbage Collection and Finalization 385
Resource Utilization and Finalization Guidelines 387
Contents xvii
10 Exception Handling 389
Multiple Exception Types 389
Catching Exceptions 391
General Catch Block 392
Guidelines for Exception Handling 395
Defining Custom Exceptions 397
11 Generics 405

C# without Generics 406
Introducing Generic Types 411
Using a Generic Class 411
Defining a Simple Generic Class 413
Benefits of Generics 414
Type Parameter Naming Guidelines 415
Generic Interfaces and Structs 416
Defining a Constructor and a Finalizer 418
Specifying a Default Value 419
Multiple Type Parameters 420
Nested Generic Types 421
Type Compatibility between Generic Classes with Type-Compatible
Type Parameters 422
Constraints 423
Interface Constraints 425
Base Class Constraints 428
struct/class Constraints 428
Multiple Constraints 429
Constructor Constraints 430
Constraint Inheritance 431
Generic Methods 436
Type Inferencing 437
Specifying Constraints 438
Generic Internals 440
Instantiating Generics Based on Value Types 441
Instantiating Generics Based on Reference Types 442
12 Delegates and Lambda Expressions 445
Introducing Delegates 446
Defining the Scenario 446
Delegate Data Types 448

Delegate Internals 449
Defining a Delegate Type 450
Instantiating a Delegate 451
Contentsxviii
Anonymous Methods 456
System-Defined Delegates: Func<> 459
Lambda Expressions 460
Statement Lambdas 460
Expression Lambdas 463
Outer Variables 469
Expression Trees 471
13 Events 479
Coding the Observer Pattern with Multicast Delegates 480
Defining Subscriber Methods 480
Defining the Publisher 482
Hooking Up the Publisher and Subscribers 483
Invoking a Delegate 484
Check for null 485
Delegate Operators 486
Sequential Invocation 488
Error Handling 491
Method Returns and Pass-By-Reference 493
Events 495
Why Events? 495
Declaring an Event 497
Coding Conventions 498
Generics and Delegates 500
Customizing the Event Implementation 504
14 Collection Interfaces with Standard Query Operators 507
Anonymous Types and Implicit Local Variable Declaration 508

Anonymous Types 508
Implicitly Typed Local Variables 510
More about Anonymous Types and Implicit Local Variables 512
Collection Initializers 514
What Makes a Class a Collection: IEnumerable<T> 517
foreach with Arrays 517
foreach with IEnumerable<T> 518
Do Not Modify Collections during foreach Iteration 522
Standard Query Operators 523
Filtering with Where() 526
Projecting with Select() 528
Deferred Execution 530
Sorting with OrderBy() and ThenBy() 534
Performing an Inner Join with Join() 540
Implementing a One-to-Many Relationship with GroupJoin() 543
Contents xix
Calling SelectMany() 547
More Standard Query Operators 548
15 Query Expressions 555
Introducing Query Expressions 556
Projection 558
Filtering 564
Sorting 565
Let 566
Grouping 568
Query Expressions as Method Invocations 573
16 Building Custom Collections 577
More Collection Interfaces 578
IList<T> versus IDictionary<TKey, TValue> 579
IComparable<T> 580

ICollection<T> 582
Primary Collection Classes 583
List Collections: List<T> 583
Dictionary Collections: Dictionary<TKey, TValue> 588
Sorted Collections: SortedDictionary<TKey, TValue>
and SortedList<T> 593
Stack Collections: Stack<T> 594
Queue Collections: Queue<T> 595
Linked Lists: LinkedList<T> 597
Providing an Index Operator 597
Returning Null or an Empty Collection 601
Iterators 601
Defining an Iterator 602
Iterator Syntax 603
Yielding Values from an Iterator 604
Iterators and State 606
More Iterator Examples 607
Placing a yield return within a Loop 609
Canceling Further Iteration: yield break 612
Creating Multiple Iterators in a Single Class 614
yield Statement Characteristics 615
17 Reflection and Attributes 617
Reflection 618
Accessing Metadata Using System.Type 619
Member Invocation 621
Reflection on Generic Types 626
Contentsxx
Attributes 629
Custom Attributes 633
Looking for Attributes 633

Initializing an Attribute through a Constructor 634
System.AttributeUsageAttribute 639
Named Parameters 641
18 Multithreading 657
Running and Controlling a Separate Thread 660
Starting a Thread 662
Thread Management 663
Passing Parameters to Threads 665
Thread Pooling 669
Unhandled Exceptions 670
Synchronization 672
Synchronization Using Monitor 675
Using the lock Keyword 677
Choosing a lock Object 679
Why to Avoid Locking on this and typeof(type) 680
Declaring Fields as volatile 680
Using the System.Threading.Interlocked Class 681
Event Notification with Multiple Threads 682
Synchronization Design Best Practices 684
More Synchronization Types 685
Timers 691
19 Multithreading Patterns 699
Asynchronous Results Pattern 700
Introducing the Asynchronous Results Pattern 701
Passing Data to and from an Alternate Thread 703
Receiving Notification of Thread Completion 708
Passing Arbitrary State 711
Asynchronous Results Conclusions 713
Background Worker Pattern 714
Establishing the Pattern 717

Exception Handling 718
Windows Forms 719
20 Platform Interoperability and Unsafe Code 723
Platform Invoke 724
Declaring External Functions 725
Parameter Data Types 726
Contents xxi
Using ref Rather Than Pointers 727
Using StructLayoutAttribute for Sequential Layout 728
Error Handling 729
Using SafeHandle 731
Calling External Functions 734
Simplifying API Calls with Wrappers 736
Function Pointers Map to Delegates 737
Guidelines 737
Pointers and Addresses 738
Unsafe Code 738
Pointer Declaration 740
Assigning a Pointer 742
Dereferencing a Pointer 744
Accessing the Member of a Referent Type 746
21 The Common Language Infrastructure 749
Defining the Common Language Infrastructure (CLI) 750
CLI Implementations 751
C# Compilation to Machine Code 752
Runtime 755
Garbage Collection 755
Garbage Collection on .NET 756
Type Safety 757
Code Access Security 758

Platform Portability 758
Performance 759
Application Domains 760
Assemblies, Manifests, and Modules 761
Common Intermediate Language (CIL) 764
Common Type System (CTS) 764
Common Language Specification (CLS) 765
Base Class Library (BCL) 766
Metadata 766
A Downloading and Installing the C# Compiler and the CLI
Platform 771
B Full Source Code Listings 775
C C# 3.0 Topics 801
Index 807
This page intentionally left blank
xxiii
Figures
Figure 2.1: Value Types Contain the Data Directly 56
Figure 2.2: Reference Types Point to the Heap 57
Figure 3.1: Corresponding Placeholder Values 114
Figure 3.3: Calculating the Value of a Signed Byte 115
Figure 3.2: Calculating the Value of an Unsigned Byte 115
Figure 3.4: The Numbers 12 and 7 Represented in Binary 117
Figure 3.5: Collapsed Region in Microsoft Visual Studio .NET 143
Figure 4.1: Exception-Handling Program Flow 184
Figure 5.1: Class Hierarchy 198
Figure 6.1: Refactoring into a Base Class 263
Figure 6.2: Working around Multiple Inheritance Using Aggregation 271
Figure 7.1: Working around Single Inheritances with Aggregation and
Interfaces 315

Figure 8.1: Value Types Contain the Data Directly 322
Figure 8.2: Reference Types Point to the Heap 323
Figure 9.1: Identity 351
Figure 9.2: XML Comments as Tips in Visual Studio IDE 374
Figure 12.1: Delegate Types Object Model 450
Figure 12.2: Anonymous Function Terminology 460
Figures
Figuresxxiv
Figure 13.1: Delegate Invocation Sequence Diagram 489
Figure 13.2: Multicast Delegates Chained Together 490
Figure 13.3: Delegate Invocation with Exception Sequence Diagram 492
Figure 14.1:
IEnumerator<T> and IEnumerator Interfaces 519
Figure 14.2:
IEnumerator<T> and IEnumerator Interfaces 532
Figure 14.3: Venn Diagram of Inventor and Patent Collections 537
Figure 16.1: Generic Collection Interface Hierarchy 579
Figure 16.2:
List<> Class Diagrams 585
Figure 16.3:
Dictionary Class Diagrams 589
Figure 16.4:
SortedList<> and SortedDictionary<> Class
Diagrams 592
Figure 16.5:
Stack<T> Class Diagrams 595
Figure 16.6:
Queue<T> Class Diagrams 596
Figure 16.7:
LinkedList<T> and LinkedListNode<T> Class Diagram 596

Figure 16.8: Sequence Diagram with
yield return 607
Figure 17.1:
MemberInfo Derived Classes 626
Figure 17.2:
BinaryFormatter Does Not Encrypt Data 649
Figure 19.1: Delegate Parameter Distribution to
BeginInvoke() and
EndInvoke() 708
Figure 20.1: Pointers Contain the Address of the Data 740
Figure 21.1: Compiling C# to Machine Code 754
Figure 21.2: Assemblies with the Modules and Files They Reference 762

×