Search and Find
Service
Title Page
2
Copyright Page
3
Dedication Page
4
Contents at a Glance
5
Table of Contents
7
About the Author
21
About the Technical Reviewer
22
Acknowledgments
23
Preface
24
About This Book
25
Chapter 1: C# Preview
28
Differences Between C# and C++
28
C#
28
C++
29
CLR Garbage Collection
30
Example of a C# Program
30
Overview of Features Added in C# 2.0
32
Overview of Features Added in C# 3.0
33
Overview of New C# 4.0 Features
34
Summary
34
Chapter 2: C# and the CLR
36
The JIT Compiler in the CLR
37
Assemblies and the Assembly Loader
38
Minimizing the Working Set of the Application
39
Naming Assemblies
39
Loading Assemblies
40
Metadata
40
Cross-Language Compatibility
42
Summary
42
Chapter 3: C# Syntax Overview
43
C# Is a Strongly Typed Language
43
Expressions
44
Statements and Expressions
46
Types and Variables
47
Value Types
49
Enumerations
50
Flags Enumerations
51
Reference Types
52
Default Variable Initialization
53
Implicitly Typed Local Variables
54
THE COMPLEXITY OF ADDING NEW KEYWORDS TO THE LANGUAGE
55
Type Conversion
56
Array Covariance
57
Boxing Conversion
57
as and is Operators
58
Generics
60
Namespaces
61
Defining Namespaces
62
Using Namespaces
63
Control Flow
65
if-else, while, do-while, and for
65
switch
65
foreach
66
break, continue, goto, return, and throw
67
Summary
67
Chapter 4: Classes, Structs, and Objects
68
Class Definitions
70
Fields
71
Constructors
74
Methods
74
Static Methods
75
Instance Methods
75
Properties
76
Declaring Properties
76
Accessors
78
Read-Only and Write-Only Properties
78
Auto-Implemented Properties
79
Encapsulation
81
Accessibility
84
Interfaces
86
Inheritance
87
Accessibility of Members
88
Implicit Conversion and a Taste of Polymorphism
88
Member Hiding
90
The base Keyword
93
sealed Classes
94
abstract Classes
95
Nested Classes
96
Indexers
99
partial Classes
101
partial Methods
102
Static Classes
104
THE SINGLETON PATTERN
105
Reserved Member Names
106
Reserved Names for Properties
106
Reserved Names for Indexers
106
Reserved Names for Destructors
106
Reserved Names for Events
107
Value Type Definitions
107
Constructors
107
The Meaning of this
109
Finalizers
112
Interfaces
112
Anonymous Types
113
Object Initializers
116
Boxing and Unboxing
119
When Boxing Occurs
123
Efficiency and Confusion
125
System.Object
126
Equality and What It Means
127
The IComparable Interface
128
Creating Objects
128
The new Keyword
128
Using new with Value Types
128
Using new with Class Types
128
Field Initialization
129
Static (Class) Constructors
131
Instance Constructor and Creation Ordering
134
Destroying Objects
138
Finalizers
138
Deterministic Destruction
139
Exception Handling
140
Disposable Objects
141
The IDisposable Interface
141
The using Keyword
143
Method Parameter Types
144
Value Arguments
145
ref Arguments
145
out Parameters
147
param Arrays
148
Method Overloading
148
Optional Arguments
149
Named Arguments
150
Inheritance and Virtual Methods
153
Virtual and Abstract Methods
154
override and new Methods
154
sealed Methods
156
A Final Few Words on C# Virtual Methods
157
Inheritance, Containment, and Delegation
157
Choosing Between Interface and Class Inheritance
157
Delegation and Composition vs. Inheritance
159
Summary
161
Chapter 5: Interfaces and Contracts
162
Interfaces Define Types
163
Defining Interfaces
164
INTERFACES DEFINE CONTRACTS
164
What Can Be in an Interface?
164
Interface Inheritance and Member Hiding
165
Implementing Interfaces
168
Implicit Interface Implementation
168
Explicit Interface Implementation
168
Overriding Interface Implementations in Derived Classes
170
Beware of Side Effects of Value Types Implementing Interfaces
175
PREFER THE CONVERT CLASS OVER ICONVERTIBLE
175
Interface Member Matching Rules
175
Explicit Interface Implementation with Value Types
179
Versioning Considerations
181
Contracts
182
Contracts Implemented with Classes
182
Interface Contracts
184
Choosing Between Interfaces and Classes
185
Summary
189
Chapter 6: Overloading Operators
190
Just Because You Can Doesn’t Mean You Should
190
Types and Formats of Overloaded Operators
190
Operators Shouldn’t Mutate Their Operands
192
Does Parameter Order Matter?
192
Overloading the Addition Operator
193
Operators That Can Be Overloaded
194
Comparison Operators
195
Conversion Operators
198
THE FLOATING POINT ENIGMA
201
Boolean Operators
201
Summary
204
Chapter 7: Exception Handling and Exception Safety
205
How the CLR Treats Exceptions
205
Mechanics of Handling Exceptions in C#
206
Throwing Exceptions
206
Changes with Unhandled Exceptions Starting with .NET 2.0
206
Syntax Overview of the try, catch, and finally Statements
207
Rethrowing Exceptions and Translating Exceptions
210
Exceptions Thrown in finally Blocks
213
Exceptions Thrown in Finalizers
213
Exceptions Thrown in Static Constructors
215
Who Should Handle Exceptions?
216
Avoid Using Exceptions to Control Flow
217
Achieving Exception Neutrality
217
Basic Structure of Exception-Neutral Code
218
Constrained Execution Regions
223
Critical Finalizers and SafeHandle
225
Creating Custom Exception Classes
230
Working with Allocated Resources and Exceptions
231
Providing Rollback Behavior
235
Summary
238
Chapter 8: Working with Strings
239
String Overview
239
String Literals
240
Format Specifiers and Globalization
241
Object.ToString, IFormattable, and CultureInfo
242
Creating and Registering Custom CultureInfo Types
243
Format Strings
245
Console.WriteLine and String.Format
246
Examples of String Formatting in Custom Types
247
ICustomFormatter
248
Comparing Strings
251
Working with Strings from Outside Sources
252
StringBuilder
254
Searching Strings with Regular Expressions
256
Searching with Regular Expressions
256
Searching and Grouping
258
Replacing Text with Regex
262
Regex Creation Options
264
Summary
266
Chapter 9: Arrays, Collection Types, and Iterators
267
Introduction to Arrays
267
Implicitly Typed Arrays
268
Type Convertibility and Covariance
271
Sortability and Searchability
272
Synchronization
273
Vectors vs. Arrays
273
Multidimensional Rectangular Arrays
275
Multidimensional Jagged Arrays
277
Collection Types
279
Comparing ICollection
with ICollection 279
Collection Synchronization
281
Lists
282
Dictionaries
283
Sets
283
System.Collections.ObjectModel
284
Efficiency
286
IEnumerable
, IEnumerator , IEnumerable, and IEnumerator 288
Types That Produce Collections
291
Iterators
292
Forward, Reverse, and Bidirectional Iterators
297
Collection Initializers
301
Summary
302
Chapter 10: Delegates, Anonymous Functions, and Events
303
Overview of Delegates
303
Delegate Creation and Use
304
Single Delegate
305
Delegate Chaining
306
Iterating Through Delegate Chains
308
Unbound (Open Instance) Delegates
309
Events
312
Anonymous Methods
316
Captured Variables and Closures
319
Beware the Captured Variable Surprise
321
Anonymous Methods as Delegate Parameter Binders
324
The Strategy Pattern
328
Summary
329
Chapter 11: Generics
330
Difference Between Generics and C++ Templates
331
Efficiency and Type Safety of Generics
332
GENERIC TYPE PLACEHOLDER NAMING CONVENTIONS
334
Generic Type Definitions and Constructed Types
334
Generic Classes and Structs
334
Generic Interfaces
337
Generic Methods
338
Generic Delegates
340
Generic Type Conversion
343
Default Value Expression
344
Nullable Types
346
Constructed Types Control Accessibility
348
Generics and Inheritance
348
Constraints
350
Constraints on Nonclass Types
355
Co- and Contravariance
355
Covariance
357
Contravariance
360
Invariance
362
Variance and Delegates
363
Generic System Collections
367
Generic System Interfaces
368
Select Problems and Solutions
370
Conversion and Operators within Generic Types
370
Creating Constructed Types Dynamically
380
Summary
381
Chapter 12: Threading in C#
383
Threading in C# and .NET
383
Starting Threads
384
Passing Data to New Threads
385
Using ParameterizedThreadStart
387
The IOU Pattern and Asynchronous Method Calls
388
States of a Thread
388
Terminating Threads
391
Halting Threads and Waking Sleeping Threads
393
Waiting for a Thread to Exit
394
Foreground and Background Threads
394
Thread-Local Storage
395
How Unmanaged Threads and COM Apartments Fit In
399
Synchronizing Work Between Threads
400
Lightweight Synchronization with the Interlocked Class
401
INTERLOCKED METHODS ON SMP SYSTEMS
403
SpinLock Class
407
Monitor Class
409
Beware of Boxing
413
Pulse and Wait
414
Locking Objects
418
ReaderWriterLock
419
ReaderWriterLockSlim
422
Mutex
423
Semaphore
424
Events
426
Win32 Synchronization Objects and WaitHandle
427
Using ThreadPool
429
Asynchronous Method Calls
430
Timers
438
Concurrent Programming
439
Task Class
440
Parallel Class
442
Easy Entry to the Thread Pool
447
Thread-Safe Collection Classes
448
Summary
448
Chapter 13: In Search of C# Canonical Forms
450
Reference Type Canonical Forms
450
Default to sealed Classes
451
Use the Non-Virtual Interface (NVI) Pattern
452
Is the Object Cloneable?
455
Is the Object Disposable?
461
Does the Object Need a Finalizer?
464
What Does Equality Mean for This Object?
471
Reference Types and Identity Equality
472
Value Equality
475
Overriding Object.Equals for Reference Types
475
If You Override Equals, Override GetHashCode Too
478
Does the Object Support Ordering?
482
Is the Object Formattable?
484
Is the Object Convertible?
488
Prefer Type Safety at All Times
490
Using Immutable Reference Types
494
Value Type Canonical Forms
497
Override Equals for Better Performance
498
Do Values of This Type Support Any Interfaces?
502
Implement Type-Safe Forms of Interface Members and Derived Methods
503
Summary
505
Checklist for Reference Types
506
Checklist for Value Types
507
Chaper 14: Extension Methods
509
Introduction to Extension Methods
509
How Does the Compiler Find Extension Methods?
510
Under the Covers
513
Code Readability versus Code Understandability
514
Recommendations for Use
515
Consider Extension Methods Over Inheritance
515
Isolate Extension Methods in Separate Namespace
516
Changing a Type’s Contract Can Break Extension Methods
517
Transforms
517
Operation Chaining
522
Custom Iterators
523
Borrowing from Functional Programming
525
The Visitor Pattern
531
Summary
535
Chaper 15: Lambda Expressions
536
Introduction to Lambda Expressions
536
Lambda Expressions and Closures
537
Closures in C# 1.0
540
Closures in C# 2.0
542
Lambda Statements
543
Expression Trees
543
Operating on Expressions
546
Functions as Data
547
Useful Applications of Lambda Expressions
548
Iterators and Generators Revisited
548
More on Closures (Variable Capture) and Memoization
552
Currying
557
Anonymous Recursion
559
Summary
560
Chaper 16: LINQ: Language Integrated Query
562
A Bridge to Data
563
Query Expressions
563
Extension Methods and Lambda Expressions Revisited
565
Standard Query Operators
566
C# Query Keywords
568
The from Clause and Range Variables
568
The join Clause
569
The where Clause and Filters
571
The orderby Clause
572
The select Clause and Projection
573
The let Clause
575
The group Clause
576
The into Clause and Continuations
579
The Virtues of Being Lazy
581
C# Iterators Foster Laziness
581
Subverting Laziness
582
Executing Queries Immediately
584
Expression Trees Revisited
585
Techniques from Functional Programming
585
Custom Standard Query Operators and Lazy Evaluation
585
Replacing foreach Statements
594
Summary
595
Chaper 17: Dynamic Types
596
What does dynamic Mean?
596
How Does dynamic Work?
599
The Great Unification
601
Call Sites
601
Objects with Custom Dynamic Behavior
604
Efficiency
606
Boxing with Dynamic
608
Dynamic Conversions
609
Implicit Dynamic Expressions Conversion
610
Dynamic Overload Resolution
611
Dynamic Inheritance
613
You Cannot Derive from dynamic
614
You Cannot Implement dynamic Interfaces
614
You Can Derive From Dynamic Base Types
616
Duck Typing in C#
618
Limitations of dynamic Types
621
ExpandoObject: Creating Objects Dynamically
621
Summary
626
Index
627
All prices incl. VAT