Title Page....7
Copyright....8
Dedication....9
About the Author and Technical Reviewer....10
Acknowledgments....11
Introduction....12
Who Should Read This Book....13
Organization and Requirements....13
Modern Features....15
Why Value Types?....15
1. Making the Most of the Type System....17
The Value of Good Names....17
Adding Clarity Through Types....19
Named Arguments....20
Custom Types....20
Encapsulation....21
Immutability....22
Value Validation....23
Testing....24
Refactoring....24
Replacing Magic Numbers with Named Constants....25
Simplifying Properties and Values....26
Overloading Arithmetic Operators....29
Determining a Need for New Types....30
Encoding Units....33
Itemizing Units with enums....33
Static Creation Methods....36
Symmetry in Design....38
Making Units Explicit....38
Choosing the Most Natural Usage....39
Returning Types Implied by Units....41
A Fully Formed Encapsulated Value....42
Deciding Whether to Abstract Types....42
Summary....43
2. Value and Reference Types....45
User-Defined Types....45
Structs and Classes....45
Records and Record Structs....46
Inheritance....47
Type Instance Lifetimes....49
Variables....50
Variables vs. Values....51
Definite Assignment....52
Instances and Storage....52
Embedded Values....52
Boxed Values....56
Semantics and Type....57
The Common Type System....57
Copy Semantics....58
Records, Structs, and Value Semantics....60
Construction and Initialization....61
Default Initialization....62
Instance Constructors....62
Field Initializers....69
Object Initializers....69
null Values and Default Values....71
Generics and null....72
Generics and Default Values....73
Nullable Value Types....74
Nullable Reference Types....75
The Null-Forgiving Operator....77
Summary....77
3. Reference and Value Parameters....79
Method Parameters and Arguments....79
Reference Types vs. By-Reference Parameters....80
Value Types and Parameters....81
The Value of a Reference....82
Reference Variables and Aliasing....83
Mutable By-Reference Parameters....85
Passing References by Reference....86
Passing Values by Reference....87
Working with Output Parameters....88
Limitations of By-Reference Parameters....91
Property Values....91
Overloading on By-Reference Parameters....92
Using Fields....93
Extension Methods....95
Side Effects and Direct Effects....97
Mutation vs. Creation....98
Declarative Code and Performance....100
Read-Only References and Returning by Reference....100
Returning Values by Reference....102
Preventing Modifications to Data....104
Keeping By-Reference Variables Within Scope....105
Considering Performance vs. Simplicity....109
Final Word on Mutable By-Reference Parameters....109
Summary....111
4. Implicit and Explicit Copying....112
Copying by Simple Assignment....112
Value Copy Behavior....113
Read-Only Properties vs. Immutable Types....115
Creating New Objects....116
Overwriting a Value....117
Constructing Value Types....118
Copying Records Like Value Types....120
Identifying Unnecessary Boxing....121
To an Interface....122
In Method Calls....123
Method Parameters and Arguments....124
Passing and Returning by Value....124
Accessing Properties....126
Using Expressions with Operators....127
Modifying Return Type Instances....129
Reference Type Properties....130
Instance Methods and Mutability....131
Properties as Arguments for Read-Only Parameters....132
Defensive Copies....133
Mutable Value Types and in Parameters....134
Automatic vs. Nonautomatic Properties....134
Read-Only Reference Variables....136
Read-Only Fields....137
Defending Against Mutation....138
Read-Only Accessors and Methods....138
Read-Only Types....139
Summary....140
5. Types of Equality....142
Built-in Equality....142
Whole Numbers....143
Floating-Point Values....144
Reference Equality....147
Strings and Value Equality....150
Custom Equality for Classes....151
Defining Equality Operators....152
Handling Comparisons with null....152
Making Type-Safe Comparisons....153
Working with Hash Codes....154
Structs and Equality....157
Overriding Equals for Structs....157
Boxing Values and Comparing by Identity....159
Comparing Generic Variables....163
Generic Code and the Equals Method....164
The IEquatable Interface....164
Compiler-Generated Equality....166
Records and Record Structs....166
Equality for Nullable Values....168
Value Tuples and Equality....170
Summary....172
6. The Nature of Values....174
Value vs. Reference Semantics....174
Copying and Equality Comparison Behavior....175
Mutability....179
Mechanics vs. Semantics....180
Object Relationships....181
Kinds of Objects....181
Object Characteristics....182
Design Refinement to Model Object Roles....188
Abstraction and Vocabulary....188
Encapsulation and Cohesion....189
Eliminating Duplication....190
Establishing Class Invariants....191
Clarifying with Symmetry....192
Encapsulation and the Public Interface....193
Extending the Interface....193
Reducing the Internal Interface....194
Composing Abstractions....195
Choosing Between Value and Reference Semantics....196
Avoiding the Pitfalls of Default Variables....196
Implementing Custom vs. Generated Behavior....197
Overriding Generated Methods....198
Comparison for Ordering....199
Equivalence vs. Equality....200
The Contract for Comparisons....201
Other Kinds of Ordering....201
The Perils of Uniformity and Consistency....203
Arithmetic and Nonarithmetic Types....203
Nonstandard Operator Behavior....204
Summary....205
7. Value Types and Polymorphism....206
Why Value Types Are Sealed....206
Implementation Inheritance....207
Value-Based Equality for Classes....208
Equality Behavior in Derived Classes....212
Equality Comparisons and Type Substitution....213
Inclusion Polymorphism and Subtyping....215
Working with Input and Output Types of Virtual Methods....216
Upholding a Type’s Contract....217
Inheriting Record Types....218
Avoiding Implementation Inheritance....223
Containing Instead of Inheriting Types....224
Parametric Polymorphism with Generics....225
Generic Constraints and Protocol Interfaces....226
Generic Method Parameters and Type Deduction....229
Parameterized Types....230
Ad Hoc Polymorphism with Overloading....231
Symbolic Polymorphism with Overloaded Operators....232
Generic Delegates for Polymorphism....233
Coercion Polymorphism Using Conversions....235
Widening vs. Narrowing Conversions....236
For Representation....236
For Purpose....237
Summary....239
8. Performance and Efficiency....241
Measuring and Optimizing Performance....241
The JIT Compiler....242
Performance Benchmarks....242
The Profiler....243
Measuring Basic Performance with Equals....244
Hidden Costs of Simplicity....246
The ValueType.Equals Method....247
The ValueType.GetHashCode Method....249
The HashCode.Combine Method....250
Optimizing Equality....251
The Effect of IEquatable....253
Property Accesses....254
The Equality Operators....255
How Type Affects Performance....259
Measuring the Cost of Copying....259
Copying Large Instances....260
Weighing Object Construction Costs....261
Measuring the Compiler-Generated Equals Method....264
How Common Idioms and Practices Affect Performance....266
Looping and Iteration....267
Pattern Matching and Selection....271
Summary....272
Afterword....274
Appendix: Further Reading....276
Index....287
The type system is the foundation upon which all C# programs are built. The C# Type System will show you how todefine and implement value types effectively, and write more performant and robust code. Real-world code examplesand test cases throughout will elevate your programming with C# and show you how best to implement the principlesyou’re learning.
Whether you’re a novice or seasoned programmer, you’ll find The C# Type System indispensable in your efforts to turngood code into great.