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

A programmer’s guide to c 5 0

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 (3.53 MB, 442 trang )


A Programmer’s Guide
to C# 5.0

Eric Gunnerson


A Programmer’s Guide to C# 5.0
Copyright © 2012 by Eric Gunnerson
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material
is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting,
reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval,
electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter
developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly
analysis or material supplied specifically for the purpose of being entered and executed on a computer system,
for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only
under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission
for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the
Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.
ISBN 978-1-4302-4593-3
ISBN 978-1-4302-4594-0 (eBook)
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every
occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial
fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither
the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may
be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
President and Publisher: Paul Manning


Lead Editor: Gwenan Spearing
Technical Reviewer: Jonathan Allen
Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel, Jonathan
Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie,
Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing,
Matt Wade, Tom Welsh
Coordinating Editor: Kevin Shea
Copy Editor: Kim Wimpsett
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th
Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail ,
or visit www.springeronline.com.
For information on translations, please e-mail , or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.
eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk
Sales–eBook Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary materials referenced by the author in this text is available to
readers at www.apress.com. For detailed information about how to locate your book’s source code, go to
www.apress.com/source-code.


To Tony Jongejan, for being ahead of his time and introducing me to programming.
—Eric Gunnerson


Contents at a Glance
Preface����������������������������������������������������������������������������������������������������������������������� xxv

About the Author������������������������������������������������������������������������������������������������������ xxvii
About the Technical Reviewer����������������������������������������������������������������������������������� xxix
Acknowledgments����������������������������������������������������������������������������������������������������� xxxi
Introduction������������������������������������������������������������������������������������������������������������� xxxiii
■■Chapter 1: C# and the .NET Runtime and Libraries����������������������������������������������������� 1
■■Chapter 2: C# QuickStart and Developing in C#���������������������������������������������������������� 3
■■Chapter 3: Classes 101���������������������������������������������������������������������������������������������� 11
■■Chapter 4: Base Classes and Inheritance������������������������������������������������������������������ 19
■■Chapter 5: Exception Handling���������������������������������������������������������������������������������� 33
■■Chapter 6: Member Accessibility and Overloading��������������������������������������������������� 47
■■Chapter 7: Other Class Details����������������������������������������������������������������������������������� 57
■■Chapter 8: Structs (Value Types)������������������������������������������������������������������������������� 77
■■Chapter 9: Interfaces������������������������������������������������������������������������������������������������� 83
■■Chapter 10: Versioning and Aliases��������������������������������������������������������������������������� 95
■■Chapter 11: Statements and Flow of Execution������������������������������������������������������� 101
■■Chapter 12: Variable Scoping and Definite Assignment������������������������������������������ 109
■■Chapter 13: Operators and Expressions������������������������������������������������������������������ 115
■■Chapter 14: Conversions����������������������������������������������������������������������������������������� 127
■■Chapter 15: Arrays�������������������������������������������������������������������������������������������������� 137
■■Chapter 16: Properties�������������������������������������������������������������������������������������������� 143
v


■ Contents at a Glance

■■Chapter 17: Generic Types��������������������������������������������������������������������������������������� 153
■■Chapter 18: Indexers, Enumerators, and Iterators�������������������������������������������������� 165
■■Chapter 19: Strings������������������������������������������������������������������������������������������������� 177
■■Chapter 20: Enumerations��������������������������������������������������������������������������������������� 187
■■Chapter 21: Attributes��������������������������������������������������������������������������������������������� 195

■■Chapter 22: Delegates, Anonymous Methods, and Lambdas����������������������������������� 203
■■Chapter 23: Events�������������������������������������������������������������������������������������������������� 215
■■Chapter 24: Dynamic Typing������������������������������������������������������������������������������������ 223
■■Chapter 25: User-Defined Conversions�������������������������������������������������������������������� 227
■■Chapter 26: Operator Overloading��������������������������������������������������������������������������� 241
■■Chapter 27: Nullable Types�������������������������������������������������������������������������������������� 247
■■Chapter 28: Linq to Objects������������������������������������������������������������������������������������� 251
■■Chapter 29: Linq to XML������������������������������������������������������������������������������������������ 269
■■Chapter 30: Linq to SQL������������������������������������������������������������������������������������������� 283
■■Chapter 31: Other Language Details������������������������������������������������������������������������ 293
■■Chapter 32: Making Friends with the .NET Framework������������������������������������������� 305
■■Chapter 33: System.Array and the Collection Classes�������������������������������������������� 311
■■Chapter 34: Threading��������������������������������������������������������������������������������������������� 319
■■Chapter 35: Asynchronous and Parallel Programming������������������������������������������� 335
■■Chapter 36: Execution-Time Code Generation��������������������������������������������������������� 345
■■Chapter 37: Interop�������������������������������������������������������������������������������������������������� 351
■■Chapter 38: .NET Base Class Library Overview������������������������������������������������������� 361
■■Chapter 39: Deeper into C#�������������������������������������������������������������������������������������� 385
■■Chapter 40: Logging and Debugging Techniques���������������������������������������������������� 405
■■Chapter 41: IDEs and Utilities���������������������������������������������������������������������������������� 421
Index��������������������������������������������������������������������������������������������������������������������������� 423
vi


Contents
Preface����������������������������������������������������������������������������������������������������������������������� xxv
About the Author������������������������������������������������������������������������������������������������������ xxvii
About the Technical Reviewer����������������������������������������������������������������������������������� xxix
Acknowledgments����������������������������������������������������������������������������������������������������� xxxi
Introduction������������������������������������������������������������������������������������������������������������� xxxiii

■■Chapter 1: C# and the .NET Runtime and Libraries����������������������������������������������������� 1
■■Chapter 2: C# QuickStart and Developing in C#���������������������������������������������������������� 3
Hello, Universe�������������������������������������������������������������������������������������������������������������������������3
Namespace and Using Statements�����������������������������������������������������������������������������������������4
Namespaces and Assemblies��������������������������������������������������������������������������������������������������5
Basic Data Types���������������������������������������������������������������������������������������������������������������������5
Classes, Structs, and Interfaces����������������������������������������������������������������������������������������������7
Statements������������������������������������������������������������������������������������������������������������������������������7
Enums�������������������������������������������������������������������������������������������������������������������������������������7
Delegates and Events��������������������������������������������������������������������������������������������������������������8
Properties and Indexers����������������������������������������������������������������������������������������������������������8
Attributes���������������������������������������������������������������������������������������������������������������������������������8
Developing in C#���������������������������������������������������������������������������������������������������������������������9
Tools of Note���������������������������������������������������������������������������������������������������������������������������������������������������9

■■Chapter 3: Classes 101���������������������������������������������������������������������������������������������� 11
A Simple Class����������������������������������������������������������������������������������������������������������������������11
Member Functions����������������������������������������������������������������������������������������������������������������13
vii


■ Contents

ref and out Parameters���������������������������������������������������������������������������������������������������������14
Overloading���������������������������������������������������������������������������������������������������������������������������16
■■Chapter 4: Base Classes and Inheritance������������������������������������������������������������������ 19
The Engineer Class����������������������������������������������������������������������������������������������������������������19
Simple Inheritance����������������������������������������������������������������������������������������������������������������20
Arrays of Engineers���������������������������������������������������������������������������������������������������������������21
Virtual Functions�������������������������������������������������������������������������������������������������������������������25

Abstract Classes��������������������������������������������������������������������������������������������������������������������27
Sealed Classes and Methods������������������������������������������������������������������������������������������������31
■■Chapter 5: Exception Handling���������������������������������������������������������������������������������� 33
What’s Wrong with Return Codes?����������������������������������������������������������������������������������������33
Trying and Catching���������������������������������������������������������������������������������������������������������������34
Choosing the Catch Block�����������������������������������������������������������������������������������������������������34
Passing Exceptions on to the Caller��������������������������������������������������������������������������������������36
Caller Beware�����������������������������������������������������������������������������������������������������������������������������������������������37
Caller Confuse����������������������������������������������������������������������������������������������������������������������������������������������37
Caller Inform�������������������������������������������������������������������������������������������������������������������������������������������������38

User-Defined Exception Classes��������������������������������������������������������������������������������������������39
Finally������������������������������������������������������������������������������������������������������������������������������������40
Top-Level Exception Handlers�����������������������������������������������������������������������������������������������42
Efficiency and Overhead��������������������������������������������������������������������������������������������������������43
Design Guidelines������������������������������������������������������������������������������������������������������������������43
Exceptions Are Exceptional��������������������������������������������������������������������������������������������������������������������������43
Choosing the Right Exception for Wrapping�������������������������������������������������������������������������������������������������43
Exceptions Should be as Specific as Possible����������������������������������������������������������������������������������������������45
Retry Logic���������������������������������������������������������������������������������������������������������������������������������������������������45
Rethrowing���������������������������������������������������������������������������������������������������������������������������������������������������45
Catch Only if You Have Something Useful to Do�������������������������������������������������������������������������������������������45

viii


■ Contents

■■Chapter 6: Member Accessibility and Overloading��������������������������������������������������� 47
Class Accessibility�����������������������������������������������������������������������������������������������������������������47

Using Internal on Members���������������������������������������������������������������������������������������������������47
Expanding Internal Accessibility�������������������������������������������������������������������������������������������������������������������48

Protected�������������������������������������������������������������������������������������������������������������������������������49
Internal Protected�����������������������������������������������������������������������������������������������������������������������������������������49

The Interaction of Class and Member Accessibility���������������������������������������������������������������49
Accessability Summary���������������������������������������������������������������������������������������������������������49
Method Overloading��������������������������������������������������������������������������������������������������������������50
Method Hiding����������������������������������������������������������������������������������������������������������������������������������������������50
Better Conversions���������������������������������������������������������������������������������������������������������������������������������������52

Variable-Length Parameter Lists�������������������������������������������������������������������������������������������53
Default Arguments�����������������������������������������������������������������������������������������������������������������54
Named Arguments�����������������������������������������������������������������������������������������������������������������56
■■Chapter 7: Other Class Details����������������������������������������������������������������������������������� 57
Nested Classes����������������������������������������������������������������������������������������������������������������������57
Other Nesting������������������������������������������������������������������������������������������������������������������������������������������������58

Anonymous Types������������������������������������������������������������������������������������������������������������������58
Creation, Initialization, Destruction���������������������������������������������������������������������������������������58
Constructors�������������������������������������������������������������������������������������������������������������������������������������������������58
Initialization��������������������������������������������������������������������������������������������������������������������������������������������������61
Destructors���������������������������������������������������������������������������������������������������������������������������������������������������61
Managing Nonmemory Resources���������������������������������������������������������������������������������������������������������������62
IDisposable and the Using Statement�����������������������������������������������������������������������������������������������������������64
IDisposable and Longer-Lived Objects���������������������������������������������������������������������������������������������������������64

Static Fields���������������������������������������������������������������������������������������������������������������������������65
Static Member Functions������������������������������������������������������������������������������������������������������66

Static Constructors����������������������������������������������������������������������������������������������������������������67
Constants������������������������������������������������������������������������������������������������������������������������������67
Read-Only Fields�������������������������������������������������������������������������������������������������������������������68
ix


■ Contents

Extension Methods����������������������������������������������������������������������������������������������������������������71
Usage Guidelines������������������������������������������������������������������������������������������������������������������������������������������72

Object Initializers�������������������������������������������������������������������������������������������������������������������72
Static Classes������������������������������������������������������������������������������������������������������������������������73
Partial Classes and Methods�������������������������������������������������������������������������������������������������74
■■Chapter 8: Structs (Value Types)������������������������������������������������������������������������������� 77
A Point Struct������������������������������������������������������������������������������������������������������������������������77
Boxing and Unboxing�������������������������������������������������������������������������������������������������������������78
Structs and Constructors������������������������������������������������������������������������������������������������������79
Mutable Structs���������������������������������������������������������������������������������������������������������������������80
Design Guidelines������������������������������������������������������������������������������������������������������������������81
Immutable Classes����������������������������������������������������������������������������������������������������������������81
■■Chapter 9: Interfaces������������������������������������������������������������������������������������������������� 83
A Simple Example�����������������������������������������������������������������������������������������������������������������83
Working with Interfaces��������������������������������������������������������������������������������������������������������84
The as Operator���������������������������������������������������������������������������������������������������������������������86
Interfaces and Inheritance����������������������������������������������������������������������������������������������������86
Design Guidelines������������������������������������������������������������������������������������������������������������������87
Multiple Implementation�������������������������������������������������������������������������������������������������������88
Explicit Interface Implementation�����������������������������������������������������������������������������������������������������������������89
Implementation Hiding���������������������������������������������������������������������������������������������������������������������������������91


Interfaces Based on Interfaces���������������������������������������������������������������������������������������������92
Interfaces and Structs�����������������������������������������������������������������������������������������������������������92
■■Chapter 10: Versioning and Aliases��������������������������������������������������������������������������� 95
A Versioning Example������������������������������������������������������������������������������������������������������������95
Coding for Versioning������������������������������������������������������������������������������������������������������������97
Type Aliases���������������������������������������������������������������������������������������������������������������������������97
External Assembly Aliases�����������������������������������������������������������������������������������������������������98
x


■ Contents

■■Chapter 11: Statements and Flow of Execution������������������������������������������������������� 101
Selection Statements����������������������������������������������������������������������������������������������������������101
If�����������������������������������������������������������������������������������������������������������������������������������������������������������������101
Switch���������������������������������������������������������������������������������������������������������������������������������������������������������102

Iteration Statements������������������������������������������������������������������������������������������������������������103
While�����������������������������������������������������������������������������������������������������������������������������������������������������������103
Do���������������������������������������������������������������������������������������������������������������������������������������������������������������104
For��������������������������������������������������������������������������������������������������������������������������������������������������������������105
Foreach�������������������������������������������������������������������������������������������������������������������������������������������������������105

Jump Statements����������������������������������������������������������������������������������������������������������������107
Break����������������������������������������������������������������������������������������������������������������������������������������������������������107
Continue�����������������������������������������������������������������������������������������������������������������������������������������������������107
Goto������������������������������������������������������������������������������������������������������������������������������������������������������������107
Return���������������������������������������������������������������������������������������������������������������������������������������������������������108


Other Statements����������������������������������������������������������������������������������������������������������������108
lock�������������������������������������������������������������������������������������������������������������������������������������������������������������108
using�����������������������������������������������������������������������������������������������������������������������������������������������������������108
try, catch, and finally����������������������������������������������������������������������������������������������������������������������������������108
checked and unchecked�����������������������������������������������������������������������������������������������������������������������������108
yield������������������������������������������������������������������������������������������������������������������������������������������������������������108

■■Chapter 12: Variable Scoping and Definite Assignment������������������������������������������ 109
Definite Assignment������������������������������������������������������������������������������������������������������������110
Definite Assignment and Class Members���������������������������������������������������������������������������������������������������112
Definite Assignment and Arrays�����������������������������������������������������������������������������������������������������������������113

■■Chapter 13: Operators and Expressions������������������������������������������������������������������ 115
Operator Precedence�����������������������������������������������������������������������������������������������������������115
Built-in Operators����������������������������������������������������������������������������������������������������������������116
User-Defined Operators�������������������������������������������������������������������������������������������������������117
Numeric Promotions������������������������������������������������������������������������������������������������������������117

xi


■ Contents

Arithmetic Operators�����������������������������������������������������������������������������������������������������������117
Unary Plus (+) ��������������������������������������������������������������������������������������������������������������������������������������������117
Unary Minus (−) �����������������������������������������������������������������������������������������������������������������������������������������117
Bitwise Complement (~) ����������������������������������������������������������������������������������������������������������������������������117
Addition (+) ������������������������������������������������������������������������������������������������������������������������������������������������117
Subtraction (−) ������������������������������������������������������������������������������������������������������������������������������������������118
Multiplication (*) ����������������������������������������������������������������������������������������������������������������������������������������118

Division (/) ��������������������������������������������������������������������������������������������������������������������������������������������������118
Remainder (%) �������������������������������������������������������������������������������������������������������������������������������������������118
Shift (<< and >>) ��������������������������������������������������������������������������������������������������������������������������������������118
Increment and Decrement (++ and --) ������������������������������������������������������������������������������������������������������118

Relational and Logical Operators����������������������������������������������������������������������������������������119
Logical Negation (!) �����������������������������������������������������������������������������������������������������������������������������������119
Relational Operators over���������������������������������������������������������������������������������������������������������������������������119
Logical Operators ��������������������������������������������������������������������������������������������������������������������������������������120
Conditional Operator (?:)�����������������������������������������������������������������������������������������������������������������������������121
Null Coalescing Operator (??)���������������������������������������������������������������������������������������������������������������������121

Assignment Operators���������������������������������������������������������������������������������������������������������121
Simple Assignment�������������������������������������������������������������������������������������������������������������������������������������122
Compound Assignment�������������������������������������������������������������������������������������������������������������������������������122

Type Operators��������������������������������������������������������������������������������������������������������������������122
typeof����������������������������������������������������������������������������������������������������������������������������������������������������������122
is�����������������������������������������������������������������������������������������������������������������������������������������������������������������123
as����������������������������������������������������������������������������������������������������������������������������������������������������������������123

Checked and Unchecked Expressions���������������������������������������������������������������������������������124
Type Inference (var)�������������������������������������������������������������������������������������������������������������125
Best Practices���������������������������������������������������������������������������������������������������������������������������������������������125

xii


■ Contents


■■Chapter 14: Conversions����������������������������������������������������������������������������������������� 127
Numeric Types���������������������������������������������������������������������������������������������������������������������127
Conversions and Member Lookup��������������������������������������������������������������������������������������������������������������128
Explicit Numeric Conversions���������������������������������������������������������������������������������������������������������������������129
Checked Conversions���������������������������������������������������������������������������������������������������������������������������������130

Conversions of Classes (Reference Types)��������������������������������������������������������������������������131
From an Object to the Base Class of an Object������������������������������������������������������������������������������������������131
From an Object to an Interface the Object Implements������������������������������������������������������������������������������133
From an Object to an Interface the Object Might Implement���������������������������������������������������������������������133
From One Interface Type to Another�����������������������������������������������������������������������������������������������������������134

Conversions of Structs (Value Types)����������������������������������������������������������������������������������135
■■Chapter 15: Arrays�������������������������������������������������������������������������������������������������� 137
Array Initialization���������������������������������������������������������������������������������������������������������������137
Multidimensional and Jagged Arrays����������������������������������������������������������������������������������137
Multidimensional Arrays�����������������������������������������������������������������������������������������������������������������������������138
Jagged Arrays���������������������������������������������������������������������������������������������������������������������������������������������139

Arrays of Reference Types���������������������������������������������������������������������������������������������������140
Array Conversions���������������������������������������������������������������������������������������������������������������141
The System.Array Type��������������������������������������������������������������������������������������������������������142
Sorting and Searching��������������������������������������������������������������������������������������������������������������������������������142
Reverse�������������������������������������������������������������������������������������������������������������������������������������������������������142

■■Chapter 16: Properties�������������������������������������������������������������������������������������������� 143
Accessors����������������������������������������������������������������������������������������������������������������������������143
Properties and Inheritance��������������������������������������������������������������������������������������������������144
Using Properties������������������������������������������������������������������������������������������������������������������144
Side Effects When Setting Values����������������������������������������������������������������������������������������145

Static Properties������������������������������������������������������������������������������������������������������������������146
Property Efficiency��������������������������������������������������������������������������������������������������������������147
Property Accessibility����������������������������������������������������������������������������������������������������������148
Virtual Properties�����������������������������������������������������������������������������������������������������������������149
xiii


■ Contents

Automatic Properties�����������������������������������������������������������������������������������������������������������150
Properties vs. Fields������������������������������������������������������������������������������������������������������������150
■■Chapter 17: Generic Types��������������������������������������������������������������������������������������� 153
A List of Integers�����������������������������������������������������������������������������������������������������������������153
Constraints��������������������������������������������������������������������������������������������������������������������������156
Interface Constraints����������������������������������������������������������������������������������������������������������������������������������156
Base Class Constraints�������������������������������������������������������������������������������������������������������������������������������156
Class and Struct Constraints����������������������������������������������������������������������������������������������������������������������157
Multiple Constraints������������������������������������������������������������������������������������������������������������������������������������157

The Default Value of a Type�������������������������������������������������������������������������������������������������157
Generic Interfaces and Inheritance�������������������������������������������������������������������������������������157
Generic Methods�����������������������������������������������������������������������������������������������������������������158
Generic Delegates���������������������������������������������������������������������������������������������������������������159
Covariance and Contravariance�������������������������������������������������������������������������������������������159
Contravariance�������������������������������������������������������������������������������������������������������������������������������������������161

Generics and Efficiency�������������������������������������������������������������������������������������������������������162
Generic Naming Guidelines�������������������������������������������������������������������������������������������������162
■■Chapter 18: Indexers, Enumerators, and Iterators�������������������������������������������������� 165
Indexing with an Integer Index��������������������������������������������������������������������������������������������165

Indexing with a String Index������������������������������������������������������������������������������������������������166
Indexing with Multiple Parameters�������������������������������������������������������������������������������������167
Design Guidelines for Indexers�������������������������������������������������������������������������������������������������������������������169

Object Enumeration�������������������������������������������������������������������������������������������������������������169
Enumerators and Foreach��������������������������������������������������������������������������������������������������������������������������169
Enabling Enumeration��������������������������������������������������������������������������������������������������������������������������������170
Iterators �����������������������������������������������������������������������������������������������������������������������������������������������������172
Named Iterators������������������������������������������������������������������������������������������������������������������������������������������173
Iterators and Generic Types������������������������������������������������������������������������������������������������������������������������174
Iterators and Resource Management���������������������������������������������������������������������������������������������������������175

xiv


■ Contents

■■Chapter 19: Strings������������������������������������������������������������������������������������������������� 177
Operations���������������������������������������������������������������������������������������������������������������������������177
String Literals����������������������������������������������������������������������������������������������������������������������179
String Encodings and Conversions��������������������������������������������������������������������������������������179
Converting Objects to Strings����������������������������������������������������������������������������������������������179
An Example�������������������������������������������������������������������������������������������������������������������������179
StringBuilder�����������������������������������������������������������������������������������������������������������������������180
Regular Expressions������������������������������������������������������������������������������������������������������������181
Regular Expression Options������������������������������������������������������������������������������������������������������������������������182
More Complex Parsing��������������������������������������������������������������������������������������������������������������������������������182

■■Chapter 20: Enumerations��������������������������������������������������������������������������������������� 187
A Line-Style Enumeration����������������������������������������������������������������������������������������������������187

Enumeration Base Types�����������������������������������������������������������������������������������������������������188
Initialization�������������������������������������������������������������������������������������������������������������������������188
Bit Flag Enums��������������������������������������������������������������������������������������������������������������������190
Conversions�������������������������������������������������������������������������������������������������������������������������191
The System.Enum Type�������������������������������������������������������������������������������������������������������191
■■Chapter 21: Attributes��������������������������������������������������������������������������������������������� 195
Using Attributes�������������������������������������������������������������������������������������������������������������������195
A Few More Details�������������������������������������������������������������������������������������������������������������������������������������197

An Attribute of Your Own�����������������������������������������������������������������������������������������������������198
Attribute Usage�������������������������������������������������������������������������������������������������������������������������������������������199
Attribute Parameters����������������������������������������������������������������������������������������������������������������������������������199

Fetching Attribute Values����������������������������������������������������������������������������������������������������200
■■Chapter 22: Delegates, Anonymous Methods, and Lambdas����������������������������������� 203
Using Delegates������������������������������������������������������������������������������������������������������������������203
Delegates to Instance Members������������������������������������������������������������������������������������������205
Multicasting�������������������������������������������������������������������������������������������������������������������������206
Delegates As Static Members���������������������������������������������������������������������������������������������207
xv


■ Contents

Anonymous Methods�����������������������������������������������������������������������������������������������������������209
Lambdas������������������������������������������������������������������������������������������������������������������������������210
Implementation�������������������������������������������������������������������������������������������������������������������������������������������211

Variable Capturing���������������������������������������������������������������������������������������������������������������211
Guidelines����������������������������������������������������������������������������������������������������������������������������212

Parameter Naming��������������������������������������������������������������������������������������������������������������������������������������212
Method, Anonymous Delegate, or Lambda?�����������������������������������������������������������������������������������������������212

■■Chapter 23: Events�������������������������������������������������������������������������������������������������� 215
A Simple Example Using Delegates�������������������������������������������������������������������������������������215
Add and Remove Functions�������������������������������������������������������������������������������������������������216
Safe Event Invocation����������������������������������������������������������������������������������������������������������219
EventHandler <T>���������������������������������������������������������������������������������������������������������������220
Custom Add and Remove����������������������������������������������������������������������������������������������������220
■■Chapter 24: Dynamic Typing������������������������������������������������������������������������������������ 223
The dynamic Keyword���������������������������������������������������������������������������������������������������������223
Dynamic Failure������������������������������������������������������������������������������������������������������������������224
Generic Arithmetic with dynamic����������������������������������������������������������������������������������������224
Usage Guidelines�����������������������������������������������������������������������������������������������������������������225
■■Chapter 25: User-Defined Conversions�������������������������������������������������������������������� 227
A Simple Example���������������������������������������������������������������������������������������������������������������227
Pre- and Post-Conversions��������������������������������������������������������������������������������������������������229
Conversions Between Structs���������������������������������������������������������������������������������������������230
Classes and Pre- and Post-Conversions�����������������������������������������������������������������������������231
Design Guidelines����������������������������������������������������������������������������������������������������������������237
Implicit Conversions Are Safe Conversions������������������������������������������������������������������������������������������������237
Define the Conversion in the More Complex Type��������������������������������������������������������������������������������������237
One Conversion to and from a Hierarchy����������������������������������������������������������������������������������������������������237
Add Conversions Only As Needed���������������������������������������������������������������������������������������������������������������237
Conversions That Operate in Other Languages������������������������������������������������������������������������������������������237
xvi


■ Contents


How It Works�����������������������������������������������������������������������������������������������������������������������239
Conversion Lookup�������������������������������������������������������������������������������������������������������������������������������������239

■■Chapter 26: Operator Overloading��������������������������������������������������������������������������� 241
Unary Operators������������������������������������������������������������������������������������������������������������������241
Binary Operators�����������������������������������������������������������������������������������������������������������������241
An Example�������������������������������������������������������������������������������������������������������������������������242
Restrictions�������������������������������������������������������������������������������������������������������������������������243
Guidelines����������������������������������������������������������������������������������������������������������������������������243
A Complex Number Class����������������������������������������������������������������������������������������������������243
■■Chapter 27: Nullable Types�������������������������������������������������������������������������������������� 247
A Simple Example���������������������������������������������������������������������������������������������������������������247
Using Nullable Types�����������������������������������������������������������������������������������������������������������249
Null Coalescing��������������������������������������������������������������������������������������������������������������������250
Nullable Types and Equality�������������������������������������������������������������������������������������������������250
■■Chapter 28: Linq to Objects������������������������������������������������������������������������������������� 251
Getting Started with Linq to Objects�����������������������������������������������������������������������������������251
Filtering Data����������������������������������������������������������������������������������������������������������������������������������������������254
Transforming Data��������������������������������������������������������������������������������������������������������������������������������������254
Stringing Sequences Together��������������������������������������������������������������������������������������������������������������������255
Behind the Curtain��������������������������������������������������������������������������������������������������������������������������������������256
Query Expressions��������������������������������������������������������������������������������������������������������������������������������������258

A Sequence Method of Your Own����������������������������������������������������������������������������������������259
Sequence Method Reference����������������������������������������������������������������������������������������������259
Aggregate Methods������������������������������������������������������������������������������������������������������������������������������������259
Transformational Methods��������������������������������������������������������������������������������������������������������������������������260
Extraction Methods�������������������������������������������������������������������������������������������������������������������������������������261
Subset Methods������������������������������������������������������������������������������������������������������������������������������������������262
Ordering Methods���������������������������������������������������������������������������������������������������������������������������������������263

Whole Sequence Methods��������������������������������������������������������������������������������������������������������������������������263

xvii


■ Contents

Conditional Methods�����������������������������������������������������������������������������������������������������������������������������������264
Generator Methods�������������������������������������������������������������������������������������������������������������������������������������264
Join()�����������������������������������������������������������������������������������������������������������������������������������������������������������265
GroupBy()����������������������������������������������������������������������������������������������������������������������������������������������������266

■■Chapter 29: Linq to XML������������������������������������������������������������������������������������������ 269
Rockin’ It “Old School”��������������������������������������������������������������������������������������������������������269
Linq to XML�������������������������������������������������������������������������������������������������������������������������269
Creating XML����������������������������������������������������������������������������������������������������������������������������������������������270
Namespaces�����������������������������������������������������������������������������������������������������������������������������������������������272
Parsing XML������������������������������������������������������������������������������������������������������������������������������������������������276

■■Chapter 30: Linq to SQL������������������������������������������������������������������������������������������� 283
Connecting by Hand������������������������������������������������������������������������������������������������������������283
Query and Class Generation������������������������������������������������������������������������������������������������284
Linq to SQL��������������������������������������������������������������������������������������������������������������������������285
Joining Tables����������������������������������������������������������������������������������������������������������������������287
How It Works�����������������������������������������������������������������������������������������������������������������������287
Modifying Data��������������������������������������������������������������������������������������������������������������������289
Stored Procedures���������������������������������������������������������������������������������������������������������������290
Linq to Entities��������������������������������������������������������������������������������������������������������������������291
■■Chapter 31: Other Language Details������������������������������������������������������������������������ 293
The Main Function���������������������������������������������������������������������������������������������������������������293

Returning an Int Status�������������������������������������������������������������������������������������������������������������������������������293
Command-Line Parameters������������������������������������������������������������������������������������������������������������������������294
Multiple Mains��������������������������������������������������������������������������������������������������������������������������������������������294

Preprocessing���������������������������������������������������������������������������������������������������������������������295
Preprocessing Directives����������������������������������������������������������������������������������������������������������������������������295
Other Preprocessor Functions��������������������������������������������������������������������������������������������������������������������298

Lexical Details���������������������������������������������������������������������������������������������������������������������299
Identifiers���������������������������������������������������������������������������������������������������������������������������������������������������299
Keywords����������������������������������������������������������������������������������������������������������������������������������������������������300
xviii


■ Contents

Literals��������������������������������������������������������������������������������������������������������������������������������301
Boolean�������������������������������������������������������������������������������������������������������������������������������������������������������301
Integer��������������������������������������������������������������������������������������������������������������������������������������������������������301
Real������������������������������������������������������������������������������������������������������������������������������������������������������������301
Character����������������������������������������������������������������������������������������������������������������������������������������������������301
String����������������������������������������������������������������������������������������������������������������������������������������������������������302
Comments��������������������������������������������������������������������������������������������������������������������������������������������������303

Expanding Internal Accessibility�����������������������������������������������������������������������������������������303
■■Chapter 32: Making Friends with the .NET Framework������������������������������������������� 305
ToString( )����������������������������������������������������������������������������������������������������������������������������305
Object Equality��������������������������������������������������������������������������������������������������������������������306
Hashes and GetHashCode( )������������������������������������������������������������������������������������������������308
Design Guidelines����������������������������������������������������������������������������������������������������������������310

Value Type Guidelines���������������������������������������������������������������������������������������������������������������������������������310
Reference Type Guidelines�������������������������������������������������������������������������������������������������������������������������310

■■Chapter 33: System.Array and the Collection Classes�������������������������������������������� 311
Sorting and Searching���������������������������������������������������������������������������������������������������������311
Specifying a Sort Order������������������������������������������������������������������������������������������������������������������������������312
Multiple Sort Orders�����������������������������������������������������������������������������������������������������������������������������������313
Ad Hoc Sorting Orders��������������������������������������������������������������������������������������������������������������������������������314
Overloading Relational Operators���������������������������������������������������������������������������������������������������������������314
Advanced Use of Hash Codes���������������������������������������������������������������������������������������������������������������������316

Synchronized Collections����������������������������������������������������������������������������������������������������316
Case-Insensitive Collections�����������������������������������������������������������������������������������������������317
Collection Classes���������������������������������������������������������������������������������������������������������������317
Design Guidelines����������������������������������������������������������������������������������������������������������������318

xix


■ Contents

■■Chapter 34: Threading��������������������������������������������������������������������������������������������� 319
Data Protection and Synchronization����������������������������������������������������������������������������������319
A Slightly Broken Example�������������������������������������������������������������������������������������������������������������������������319
Protection Techniques��������������������������������������������������������������������������������������������������������������������������������323
Mutexes������������������������������������������������������������������������������������������������������������������������������������������������������325

Access Reordering and Volatile�������������������������������������������������������������������������������������������326
Singletons���������������������������������������������������������������������������������������������������������������������������������������������������329


Threads�������������������������������������������������������������������������������������������������������������������������������330
Joining��������������������������������������������������������������������������������������������������������������������������������������������������������330
Waiting with WaitHandle�����������������������������������������������������������������������������������������������������������������������������331

■■Chapter 35: Asynchronous and Parallel Programming������������������������������������������� 335
History���������������������������������������������������������������������������������������������������������������������������������335
Thread Pools ����������������������������������������������������������������������������������������������������������������������337
Introducing Tasks����������������������������������������������������������������������������������������������������������������337
Tasks with Return Values����������������������������������������������������������������������������������������������������������������������������338

The async and await Keywords�������������������������������������������������������������������������������������������338
Tasks and Completion Ports������������������������������������������������������������������������������������������������339
Tasks and Parallel Operations���������������������������������������������������������������������������������������������340
Data Parallelism������������������������������������������������������������������������������������������������������������������341
PLinq�����������������������������������������������������������������������������������������������������������������������������������343
Design Guidelines����������������������������������������������������������������������������������������������������������������343
■■Chapter 36: Execution-Time Code Generation��������������������������������������������������������� 345
Loading Assemblies������������������������������������������������������������������������������������������������������������345
Making It Dynamic��������������������������������������������������������������������������������������������������������������������������������������346

Code Generation at Runtime�����������������������������������������������������������������������������������������������348
Expressing the Algorithm in Code���������������������������������������������������������������������������������������������������������������348
Translating and Loading the Code��������������������������������������������������������������������������������������������������������������348
Calling the Code������������������������������������������������������������������������������������������������������������������������������������������348
Design Guidelines���������������������������������������������������������������������������������������������������������������������������������������349

The C# Compiler As a Service���������������������������������������������������������������������������������������������349
xx



■ Contents

■■Chapter 37: Interop�������������������������������������������������������������������������������������������������� 351
COM Objects������������������������������������������������������������������������������������������������������������������������351
Calling Native DLL Functions�����������������������������������������������������������������������������������������������352
Pointers and Declarative Pinning����������������������������������������������������������������������������������������������������������������352
Structure Layout�����������������������������������������������������������������������������������������������������������������������������������������355
Calling a Function with a Structure Parameter�������������������������������������������������������������������������������������������355
Fixed-Size Buffers��������������������������������������������������������������������������������������������������������������������������������������356
Hooking Up to a Windows Callback������������������������������������������������������������������������������������������������������������357
The Marshal Class��������������������������������������������������������������������������������������������������������������������������������������359
Design Guidelines���������������������������������������������������������������������������������������������������������������������������������������359

■■Chapter 38: .NET Base Class Library Overview������������������������������������������������������� 361
Numeric Formatting������������������������������������������������������������������������������������������������������������361
Standard Format Strings����������������������������������������������������������������������������������������������������������������������������361
Custom Format Strings�������������������������������������������������������������������������������������������������������������������������������365

Date and Time Formatting���������������������������������������������������������������������������������������������������368
Custom Object Formatting��������������������������������������������������������������������������������������������������369
Numeric Parsing������������������������������������������������������������������������������������������������������������������370
Using TryParse to Avoid Exceptions������������������������������������������������������������������������������������������������������������371

Input and Output������������������������������������������������������������������������������������������������������������������371
Binary���������������������������������������������������������������������������������������������������������������������������������������������������������372
Text�������������������������������������������������������������������������������������������������������������������������������������������������������������372
XML�������������������������������������������������������������������������������������������������������������������������������������������������������������372
Writing Files������������������������������������������������������������������������������������������������������������������������������������������������373
Reading Files����������������������������������������������������������������������������������������������������������������������������������������������373
Traversing Directories���������������������������������������������������������������������������������������������������������������������������������373

Starting Processes�������������������������������������������������������������������������������������������������������������������������������������375

Serialization������������������������������������������������������������������������������������������������������������������������376
Custom Serialization�����������������������������������������������������������������������������������������������������������378
XML Serialization�����������������������������������������������������������������������������������������������������������������380
XmlSerializer or XElement?������������������������������������������������������������������������������������������������������������������������381
xxi


■ Contents

Reading Web Pages�������������������������������������������������������������������������������������������������������������382
Accessing Environment Settings�����������������������������������������������������������������������������������������382
■■Chapter 39: Deeper into C#�������������������������������������������������������������������������������������� 385
C# Style�������������������������������������������������������������������������������������������������������������������������������385
Naming�������������������������������������������������������������������������������������������������������������������������������������������������������385

Guidelines for the Library Author����������������������������������������������������������������������������������������386
Class Naming����������������������������������������������������������������������������������������������������������������������������������������������386

Unsafe Context��������������������������������������������������������������������������������������������������������������������386
XML Documentation������������������������������������������������������������������������������������������������������������389
Compiler Support Tags�������������������������������������������������������������������������������������������������������������������������������389
Generating Real Documentation�����������������������������������������������������������������������������������������������������������������391
XML Documentation Tags���������������������������������������������������������������������������������������������������������������������������392
XML Include Files���������������������������������������������������������������������������������������������������������������������������������������392

Garbage Collection in the .NET Runtime�����������������������������������������������������������������������������393
Allocation����������������������������������������������������������������������������������������������������������������������������������������������������393
Mark and Compact�������������������������������������������������������������������������������������������������������������������������������������393

Generations������������������������������������������������������������������������������������������������������������������������������������������������394
Finalization�������������������������������������������������������������������������������������������������������������������������������������������������395
Controlling GC Behavior������������������������������������������������������������������������������������������������������������������������������396

Deeper Reflection����������������������������������������������������������������������������������������������������������������396
Listing All the Types in an Assembly�����������������������������������������������������������������������������������������������������������397
Finding Members����������������������������������������������������������������������������������������������������������������������������������������398
Invoking Functions�������������������������������������������������������������������������������������������������������������������������������������399
Dealing with Generics���������������������������������������������������������������������������������������������������������������������������������402

Optimizations����������������������������������������������������������������������������������������������������������������������403
■■Chapter 40: Logging and Debugging Techniques���������������������������������������������������� 405
Conditional Methods�����������������������������������������������������������������������������������������������������������405
Asserts��������������������������������������������������������������������������������������������������������������������������������406
Changing the Assert Behavior���������������������������������������������������������������������������������������������407
Adding Other Messages to Debug or Trace Output��������������������������������������������������������������407
xxii


■ Contents

Using Switches to Control Debug and Trace�����������������������������������������������������������������������408
BooleanSwitch��������������������������������������������������������������������������������������������������������������������������������������������409
TraceSwitch������������������������������������������������������������������������������������������������������������������������������������������������410
User-Defined Switch�����������������������������������������������������������������������������������������������������������������������������������411
Capturing Process Metadata����������������������������������������������������������������������������������������������������������������������414

Improving Your Debugger Experience���������������������������������������������������������������������������������415
The DebuggerDisplay Attribute�������������������������������������������������������������������������������������������������������������������416
Changing the Display of Existing Types������������������������������������������������������������������������������������������������������417

Changing the Detail Display for a Type�������������������������������������������������������������������������������������������������������417
Full Customization of the Detail Display�����������������������������������������������������������������������������������������������������418
Debugger Visualizers����������������������������������������������������������������������������������������������������������������������������������419

Debugging into the .NET Framework����������������������������������������������������������������������������������419
■■Chapter 41: IDEs and Utilities���������������������������������������������������������������������������������� 421
IDEs�������������������������������������������������������������������������������������������������������������������������������������421
Unit Testing and TDD�����������������������������������������������������������������������������������������������������������421
Test Utilities/Runners����������������������������������������������������������������������������������������������������������������������������������421

Disassemblers and Decompilers�����������������������������������������������������������������������������������������422
Obfuscators�������������������������������������������������������������������������������������������������������������������������422
General Productivity������������������������������������������������������������������������������������������������������������422
Regular Expressions������������������������������������������������������������������������������������������������������������422
Profilers�������������������������������������������������������������������������������������������������������������������������������422
Answering Your Questions���������������������������������������������������������������������������������������������������422
Index��������������������������������������������������������������������������������������������������������������������������� 423

xxiii


Preface
It was January 4, 1999, and the Visual C++ team had just returned from our holiday break, ready to put the
finishing touches on a new version of Visual C++.1 We had a surprise group meeting and discovered that the
entire developer division was switching gears; our release was canceled, and we were going to work on a new
thing that would become the .NET managed environment, libraries, and compilers.
I transitioned from test lead on the C++ compiler parser to test lead on a new language that we were
building—one that would be a natural fit with the new environment. At that point, the language spec was only
in an embryonic stage, and to understand how this language was supposed to work, I joined the design team.
That led to a very enjoyable period, working with a small group of very smart people. A few years later, we were

releasing the first version of C# and .NET to the world, and from the response, it was clear that developing for
Microsoft platforms would never be the same.
More than 12 years have passed since the day I first picked up the language spec. C# was the kind of
language that I always wanted to use when it was first released, and innovations such as Linq have only made it
better. I’m confident that recent additions such as asynchronous support and treating the compiler as a service
will continue that tradition.
I hope you enjoy this look behind the language.
—Eric Gunnerson

If I recall correctly, it would have been Visual C++ V6.5.

1 

xxv


About the Author
After narrowly escaping the era of punch cards, Eric Gunnerson worked
at various companies before landing at Microsoft, where he has worked on
Visual C++, Visual C#, Windows DVD Maker, Microsoft HealthVault, and, most
recently, internal tools in the Engineering Excellence group.
He was a member of the C# language design team through the
development of the early versions of the language and was the author of a
well-trafficked blog during that time.
In his spare time, he enjoys skiing and ski instruction, leading bicycle
rides, doing home improvement, working on microcontroller projects, and
writing about himself in the third person.

xxvii



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×