C# 2010 offers powerful new features, and this book is the fastest path to mastering them—and the rest of C#—for both experienced C# programmers moving to C# 2010 and programmers moving to C# from another object-oriented language. Many books introduce C#, but very few also explain how to use it optimally with the .NET Common Language Runtime (CLR). This book teaches both core C# language concepts and how to wisely employ C# idioms and object-oriented design patterns to exploit the power of C# and the CLR. This book is both a rapid tutorial and a permanent reference. You'll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way from the start? Comprehensively and concisely explains both C# 2008 and C# 2010 features Focuses on the language itself and on how to use C# 2010 proficiently for all .NET application development Concentrates on how C# features work and how to best use them for robust, high-performance code What you’ll learn How C# works with and exploits the CLR How to use arrays, collections, and iterators How to handle events with delegates and anonymous functions How to design and use generic types and methods How to thread efficiently and robustly How C# anonymous types, lamba expressions, and extension methods work and how to use them Who this book is for If you're an experienced C# programmer, you need to understand how C# has changed with C# 2010. If you're an experienced object-oriented programmer moving to C#, you want to ramp up quickly in the language while learning the latest features and techniques. In either case, this book is for you. The first three chapters succinctly present C# fundamentals, for those new to or reviewing C#. The rest of the book covers all the major C# features, in great detail, explaining how they work and how best to use them. Whatever your background or need, you’ll treasure this book for as long as you code in C# 2010. Table of Contents C# Preview C# and the CLR C# Syntax Overview Classes, Structs, and Objects Interfaces and Contracts Overloading Operators Exception Handling and Exception Safety Working with Strings Arrays, Collection Types, and Iterators Delegates, Anonymous Functions, and Events Generics Threading in C# In Search of C# Canonical Forms Extension Methods Lambda Expressions LINQ: Language Integrated Query Dynamic Types Prelim......Page 1 Contents at a Glance......Page 7 Contents......Page 9 About the Author......Page 23 About the Technical Reviewer......Page 24 Acknowledgments......Page 25 Preface......Page 26 About This Book......Page 27 C#......Page 31 C++......Page 32 Example of a C# Program......Page 33 Overview of Features Added in C# 2.0......Page 35 Overview of Features Added in C# 3.0......Page 36 Summary......Page 37 C# and the CLR......Page 39 The JIT Compiler in the CLR......Page 40 Assemblies and the Assembly Loader......Page 41 Naming Assemblies......Page 42 Metadata......Page 43 Summary......Page 45 C# Is a Strongly Typed Language......Page 47 Expressions......Page 48 Statements and Expressions......Page 50 Types and Variables......Page 51 Value Types......Page 53 Enumerations......Page 54 Flags Enumerations......Page 55 Reference Types......Page 56 Default Variable Initialization......Page 57 Implicitly Typed Local Variables......Page 58 Type Conversion......Page 60 Boxing Conversion......Page 61 as and is Operators......Page 62 Generics......Page 64 Namespaces......Page 65 Defining Namespaces......Page 66 Using Namespaces......Page 67 switch......Page 69 foreach......Page 70 Summary......Page 71 Classes, Structs, and Objects......Page 73 Class Definitions......Page 75 Fields......Page 76 Methods......Page 79 Instance Methods......Page 80 Declaring Properties......Page 81 Read-Only and Write-Only Properties......Page 83 Auto-Implemented Properties......Page 84 Encapsulation......Page 86 Accessibility......Page 89 Interfaces......Page 91 Inheritance......Page 92 Implicit Conversion and a Taste of Polymorphism......Page 93 Member Hiding......Page 95 The base Keyword......Page 98 sealed Classes......Page 99 abstract Classes......Page 100 Nested Classes......Page 101 Indexers......Page 104 partial Classes......Page 106 partial Methods......Page 107 Static Classes......Page 109 Reserved Names for Destructors......Page 111 Constructors......Page 112 The Meaning of this......Page 114 Interfaces......Page 117 Anonymous Types......Page 118 Object Initializers......Page 121 Boxing and Unboxing......Page 124 When Boxing Occurs......Page 128 Efficiency and Confusion......Page 130 System.Object......Page 131 Equality and What It Means......Page 132 Using new with Class Types......Page 133 Field Initialization......Page 134 Static (Class) Constructors......Page 136 Instance Constructor and Creation Ordering......Page 139 Finalizers......Page 143 Deterministic Destruction......Page 144 Exception Handling......Page 145 The IDisposable Interface......Page 146 The using Keyword......Page 148 Method Parameter Types......Page 149 ref Arguments......Page 150 out Parameters......Page 152 Method Overloading......Page 153 Optional Arguments......Page 154 Named Arguments......Page 155 Inheritance and Virtual Methods......Page 158 override and new Methods......Page 159 sealed Methods......Page 161 Choosing Between Interface and Class Inheritance......Page 162 Delegation and Composition vs. Inheritance......Page 164 Summary......Page 166 Interfaces and Contracts......Page 167 Interfaces Define Types......Page 168 What Can Be in an Interface?......Page 169 Interface Inheritance and Member Hiding......Page 170 Explicit Interface Implementation......Page 173 Overriding Interface Implementations in Derived Classes......Page 175 Interface Member Matching Rules......Page 180 Explicit Interface Implementation with Value Types......Page 184 Versioning Considerations......Page 186 Contracts Implemented with Classes......Page 187 Interface Contracts......Page 189 Choosing Between Interfaces and Classes......Page 190 Summary......Page 194 Types and Formats of Overloaded Operators......Page 195 Does Parameter Order Matter?......Page 197 Overloading the Addition Operator......Page 198 Operators That Can Be Overloaded......Page 199 Comparison Operators......Page 200 Conversion Operators......Page 203 Boolean Operators......Page 206 Summary......Page 209 How the CLR Treats Exceptions......Page 211 Changes with Unhandled Exceptions Starting with .NET 2.0......Page 212 Syntax Overview of the try, catch, and finally Statements......Page 213 Rethrowing Exceptions and Translating Exceptions......Page 216 Exceptions Thrown in Finalizers......Page 219 Exceptions Thrown in Static Constructors......Page 221 Who Should Handle Exceptions?......Page 222 Achieving Exception Neutrality......Page 223 Basic Structure of Exception-Neutral Code......Page 224 Constrained Execution Regions......Page 229 Critical Finalizers and SafeHandle......Page 231 Creating Custom Exception Classes......Page 236 Working with Allocated Resources and Exceptions......Page 237 Providing Rollback Behavior......Page 241 Summary......Page 244 String Overview......Page 245 String Literals......Page 246 Format Specifiers and Globalization......Page 247 Object.ToString, IFormattable, and CultureInfo......Page 248 Creating and Registering Custom CultureInfo Types......Page 249 Format Strings......Page 251 Console.WriteLine and String.Format......Page 252 Examples of String Formatting in Custom Types......Page 253 ICustomFormatter......Page 254 Comparing Strings......Page 257 Working with Strings from Outside Sources......Page 258 StringBuilder......Page 260 Searching with Regular Expressions......Page 262 Searching and Grouping......Page 264 Replacing Text with Regex......Page 268 Regex Creation Options......Page 270 Summary......Page 272 Introduction to Arrays......Page 273 Implicitly Typed Arrays......Page 274 Type Convertibility and Covariance......Page 277 Sortability and Searchability......Page 278 Vectors vs. Arrays......Page 279 Multidimensional Rectangular Arrays......Page 281 Multidimensional Jagged Arrays......Page 283 Comparing ICollection with ICollection......Page 285 Collection Synchronization......Page 287 Lists......Page 288 Sets......Page 289 System.Collections.ObjectModel......Page 290 Efficiency......Page 292 IEnumerable , IEnumerator , IEnumerable, and IEnumerator......Page 294 Types That Produce Collections......Page 297 Iterators......Page 298 Forward, Reverse, and Bidirectional Iterators......Page 303 Collection Initializers......Page 307 Summary......Page 308 Overview of Delegates......Page 309 Delegate Creation and Use......Page 310 Single Delegate......Page 311 Delegate Chaining......Page 312 Iterating Through Delegate Chains......Page 314 Unbound (Open Instance) Delegates......Page 315 Events......Page 318 Anonymous Methods......Page 322 Captured Variables and Closures......Page 325 Beware the Captured Variable Surprise......Page 327 Anonymous Methods as Delegate Parameter Binders......Page 330 The Strategy Pattern......Page 334 Summary......Page 335 Generics......Page 337 Difference Between Generics and C++ Templates......Page 338 Efficiency and Type Safety of Generics......Page 339 Generic Classes and Structs......Page 341 Generic Interfaces......Page 344 Generic Methods......Page 345 Generic Delegates......Page 347 Generic Type Conversion......Page 350 Default Value Expression......Page 351 Nullable Types......Page 353 Generics and Inheritance......Page 355 Constraints......Page 357 Coand Contravariance......Page 362 Covariance......Page 364 Contravariance......Page 367 Invariance......Page 369 Variance and Delegates......Page 370 Generic System Collections......Page 374 Generic System Interfaces......Page 375 Conversion and Operators within Generic Types......Page 377 Creating Constructed Types Dynamically......Page 387 Summary......Page 388 Threading in C# and .NET......Page 391 Starting Threads......Page 392 Passing Data to New Threads......Page 393 Using ParameterizedThreadStart......Page 395 States of a Thread......Page 396 Terminating Threads......Page 399 Halting Threads and Waking Sleeping Threads......Page 401 Foreground and Background Threads......Page 402 Thread-Local Storage......Page 403 How Unmanaged Threads and COM Apartments Fit In......Page 407 Synchronizing Work Between Threads......Page 408 Lightweight Synchronization with the Interlocked Class......Page 409 SpinLock Class......Page 415 Monitor Class......Page 417 Beware of Boxing......Page 421 Pulse and Wait......Page 422 Locking Objects......Page 426 ReaderWriterLock......Page 427 ReaderWriterLockSlim......Page 430 Mutex......Page 431 Semaphore......Page 432 Events......Page 434 Win32 Synchronization Objects and WaitHandle......Page 435 Using ThreadPool......Page 437 Asynchronous Method Calls......Page 438 Timers......Page 446 Concurrent Programming......Page 447 Task Class......Page 448 Parallel Class......Page 450 Easy Entry to the Thread Pool......Page 455 Summary......Page 456 Reference Type Canonical Forms......Page 459 Default to sealed Classes......Page 460 Use the Non-Virtual Interface (NVI) Pattern......Page 461 Is the Object Cloneable?......Page 464 Is the Object Disposable?......Page 470 Does the Object Need a Finalizer?......Page 473 What Does Equality Mean for This Object?......Page 480 Reference Types and Identity Equality......Page 481 Overriding Object.Equals for Reference Types......Page 484 If You Override Equals, Override GetHashCode Too......Page 487 Does the Object Support Ordering?......Page 491 Is the Object Formattable?......Page 493 Is the Object Convertible?......Page 497 Prefer Type Safety at All Times......Page 499 Using Immutable Reference Types......Page 503 Value Type Canonical Forms......Page 506 Override Equals for Better Performance......Page 507 Do Values of This Type Support Any Interfaces?......Page 511 Implement Type-Safe Forms of Interface Members and Derived Methods......Page 512 Summary......Page 514 Checklist for Reference Types......Page 515 Checklist for Value Types......Page 516 Introduction to Extension Methods......Page 519 How Does the Compiler Find Extension Methods?......Page 520 Under the Covers......Page 523 Code Readability versus Code Understandability......Page 524 Consider Extension Methods Over Inheritance......Page 525 Isolate Extension Methods in Separate Namespace......Page 526 Transforms......Page 527 Operation Chaining......Page 532 Custom Iterators......Page 533 Borrowing from Functional Programming......Page 535 The Visitor Pattern......Page 541 Summary......Page 545 Introduction to Lambda Expressions......Page 547 Lambda Expressions and Closures......Page 548 Closures in C# 1.0......Page 551 Closures in C# 2.0......Page 553 Expression Trees......Page 554 Operating on Expressions......Page 557 Functions as Data......Page 558 Iterators and Generators Revisited......Page 559 More on Closures (Variable Capture) and Memoization......Page 563 Currying......Page 568 Anonymous Recursion......Page 570 Summary......Page 571 LINQ: Language Integrated Query......Page 573 Query Expressions......Page 574 Extension Methods and Lambda Expressions Revisited......Page 576 Standard Query Operators......Page 577 The from Clause and Range Variables......Page 579 The join Clause......Page 580 The where Clause and Filters......Page 582 The orderby Clause......Page 583 The select Clause and Projection......Page 584 The let Clause......Page 586 The group Clause......Page 587 The into Clause and Continuations......Page 590 C# Iterators Foster Laziness......Page 592 Subverting Laziness......Page 593 Executing Queries Immediately......Page 595 Custom Standard Query Operators and Lazy Evaluation......Page 596 Replacing foreach Statements......Page 605 Summary......Page 606 What does dynamic Mean?......Page 607 How Does dynamic Work?......Page 610 Call Sites......Page 612 Objects with Custom Dynamic Behavior......Page 615 Efficiency......Page 617 Boxing with Dynamic......Page 619 Dynamic Conversions......Page 620 Implicit Dynamic Expressions Conversion......Page 621 Dynamic Overload Resolution......Page 622 Dynamic Inheritance......Page 624 You Cannot Implement dynamic Interfaces......Page 625 You Can Derive From Dynamic Base Types......Page 627 Duck Typing in C#......Page 629 ExpandoObject: Creating Objects Dynamically......Page 632 Summary......Page 637 A......Page 639 C......Page 640 D......Page 643 E......Page 644 G......Page 645 I......Page 646 L......Page 648 M......Page 649 O......Page 650 P......Page 651 R......Page 652 S......Page 653 T......Page 655 U......Page 656 Y......Page 657 Prelim 1 Contents at a Glance 7 Contents 9 About the Author 23 About the Technical Reviewer 24 Acknowledgments 25 Preface 26 About This Book 27 C# Preview 31 Differences Between C# and C++ 31 C# 31 C++ 32 CLR Garbage Collection 33 Example of a C# Program 33 Overview of Features Added in C# 2.0 35 Overview of Features Added in C# 3.0 36 Overview of New C# 4.0 Features 37 Summary 37 C# and the CLR 39 The JIT Compiler in the CLR 40 Assemblies and the Assembly Loader 41 Minimizing the Working Set of the Application 42 Naming Assemblies 42 Loading Assemblies 43 Metadata 43 Cross-Language Compatibility 45 Summary 45 C# Syntax Overview 47 C# Is a Strongly Typed Language 47 Expressions 48 Statements and Expressions 50 Types and Variables 51 Value Types 53 Enumerations 54 Flags Enumerations 55 Reference Types 56 Default Variable Initialization 57 Implicitly Typed Local Variables 58 Type Conversion 60 Array Covariance 61 Boxing Conversion 61 as and is Operators 62 Generics 64 Namespaces 65 Defining Namespaces 66 Using Namespaces 67 Control Flow 69 if-else, while, do-while, and for 69 switch 69 foreach 70 break, continue, goto, return, and throw 71 Summary 71 Classes, Structs, and Objects 73 Class Definitions 75 Fields 76 Constructors 79 Methods 79 Static Methods 80 Instance Methods 80 Properties 81 Declaring Properties 81 Accessors 83 Read-Only and Write-Only Properties 83 Auto-Implemented Properties 84 Encapsulation 86 Accessibility 89 Interfaces 91 Inheritance 92 Accessibility of Members 93 Implicit Conversion and a Taste of Polymorphism 93 Member Hiding 95 The base Keyword 98 sealed Classes 99 abstract Classes 100 Nested Classes 101 Indexers 104 partial Classes 106 partial Methods 107 Static Classes 109 Reserved Member Names 111 Reserved Names for Properties 111 Reserved Names for Indexers 111 Reserved Names for Destructors 111 Reserved Names for Events 112 Value Type Definitions 112 Constructors 112 The Meaning of this 114 Finalizers 117 Interfaces 117 Anonymous Types 118 Object Initializers 121 Boxing and Unboxing 124 When Boxing Occurs 128 Efficiency and Confusion 130 System.Object 131 Equality and What It Means 132 The IComparable Interface 133 Creating Objects 133 The new Keyword 133 Using new with Value Types 133 Using new with Class Types 133 Field Initialization 134 Static (Class) Constructors 136 Instance Constructor and Creation Ordering 139 Destroying Objects 143 Finalizers 143 Deterministic Destruction 144 Exception Handling 145 Disposable Objects 146 The IDisposable Interface 146 The using Keyword 148 Method Parameter Types 149 Value Arguments 150 ref Arguments 150 out Parameters 152 param Arrays 153 Method Overloading 153 Optional Arguments 154 Named Arguments 155 Inheritance and Virtual Methods 158 Virtual and Abstract Methods 159 override and new Methods 159 sealed Methods 161 A Final Few Words on C# Virtual Methods 162 Inheritance, Containment, and Delegation 162 Choosing Between Interface and Class Inheritance 162 Delegation and Composition vs. Inheritance 164 Summary 166 Interfaces and Contracts 167 Interfaces Define Types 168 Defining Interfaces 169 What Can Be in an Interface? 169 Interface Inheritance and Member Hiding 170 Implementing Interfaces 173 Implicit Interface Implementation 173 Explicit Interface Implementation 173 Overriding Interface Implementations in Derived Classes 175 Beware of Side Effects of Value Types Implementing Interfaces 180 Interface Member Matching Rules 180 Explicit Interface Implementation with Value Types 184 Versioning Considerations 186 Contracts 187 Contracts Implemented with Classes 187 Interface Contracts 189 Choosing Between Interfaces and Classes 190 Summary 194 Overloading Operators 195 Just Because You Can Doesn’t Mean You Should 195 Types and Formats of Overloaded Operators 195 Operators Shouldn’t Mutate Their Operands 197 Does Parameter Order Matter? 197 Overloading the Addition Operator 198 Operators That Can Be Overloaded 199 Comparison Operators 200 Conversion Operators 203 Boolean Operators 206 Summary 209 Exception Handling and Exception Safety 211 How the CLR Treats Exceptions 211 Mechanics of Handling Exceptions in C# 212 Throwing Exceptions 212 Changes with Unhandled Exceptions Starting with .NET 2.0 212 Syntax Overview of the try, catch, and finally Statements 213 Rethrowing Exceptions and Translating Exceptions 216 Exceptions Thrown in finally Blocks 219 Exceptions Thrown in Finalizers 219 Exceptions Thrown in Static Constructors 221 Who Should Handle Exceptions? 222 Avoid Using Exceptions to Control Flow 223 Achieving Exception Neutrality 223 Basic Structure of Exception-Neutral Code 224 Constrained Execution Regions 229 Critical Finalizers and SafeHandle 231 Creating Custom Exception Classes 236 Working with Allocated Resources and Exceptions 237 Providing Rollback Behavior 241 Summary 244 Working with Strings 245 String Overview 245 String Literals 246 Format Specifiers and Globalization 247 Object.ToString, IFormattable, and CultureInfo 248 Creating and Registering Custom CultureInfo Types 249 Format Strings 251 Console.WriteLine and String.Format 252 Examples of String Formatting in Custom Types 253 ICustomFormatter 254 Comparing Strings 257 Working with Strings from Outside Sources 258 StringBuilder 260 Searching Strings with Regular Expressions 262 Searching with Regular Expressions 262 Searching and Grouping 264 Replacing Text with Regex 268 Regex Creation Options 270 Summary 272 Arrays, Collection Types, and Iterators 273 Introduction to Arrays 273 Implicitly Typed Arrays 274 Type Convertibility and Covariance 277 Sortability and Searchability 278 Synchronization 279 Vectors vs. Arrays 279 Multidimensional Rectangular Arrays 281 Multidimensional Jagged Arrays 283 Collection Types 285 Comparing ICollection with ICollection 285 Collection Synchronization 287 Lists 288 Dictionaries 289 Sets 289 System.Collections.ObjectModel 290 Efficiency 292 IEnumerable , IEnumerator , IEnumerable, and IEnumerator 294 Types That Produce Collections 297 Iterators 298 Forward, Reverse, and Bidirectional Iterators 303 Collection Initializers 307 Summary 308 Delegates, Anonymous Functions, and Events 309 Overview of Delegates 309 Delegate Creation and Use 310 Single Delegate 311 Delegate Chaining 312 Iterating Through Delegate Chains 314 Unbound (Open Instance) Delegates 315 Events 318 Anonymous Methods 322 Captured Variables and Closures 325 Beware the Captured Variable Surprise 327 Anonymous Methods as Delegate Parameter Binders 330 The Strategy Pattern 334 Summary 335 Generics 337 Difference Between Generics and C++ Templates 338 Efficiency and Type Safety of Generics 339 Generic Type Definitions and Constructed Types 341 Generic Classes and Structs 341 Generic Interfaces 344 Generic Methods 345 Generic Delegates 347 Generic Type Conversion 350 Default Value Expression 351 Nullable Types 353 Constructed Types Control Accessibility 355 Generics and Inheritance 355 Constraints 357 Constraints on Nonclass Types 362 Coand Contravariance 362 Covariance 364 Contravariance 367 Invariance 369 Variance and Delegates 370 Generic System Collections 374 Generic System Interfaces 375 Select Problems and Solutions 377 Conversion and Operators within Generic Types 377 Creating Constructed Types Dynamically 387 Summary 388 Threading in C# 391 Threading in C# and .NET 391 Starting Threads 392 Passing Data to New Threads 393 Using ParameterizedThreadStart 395 The IOU Pattern and Asynchronous Method Calls 396 States of a Thread 396 Terminating Threads 399 Halting Threads and Waking Sleeping Threads 401 Waiting for a Thread to Exit 402 Foreground and Background Threads 402 Thread-Local Storage 403 How Unmanaged Threads and COM Apartments Fit In 407 Synchronizing Work Between Threads 408 Lightweight Synchronization with the Interlocked Class 409 SpinLock Class 415 Monitor Class 417 Beware of Boxing 421 Pulse and Wait 422 Locking Objects 426 ReaderWriterLock 427 ReaderWriterLockSlim 430 Mutex 431 Semaphore 432 Events 434 Win32 Synchronization Objects and WaitHandle 435 Using ThreadPool 437 Asynchronous Method Calls 438 Timers 446 Concurrent Programming 447 Task Class 448 Parallel Class 450 Easy Entry to the Thread Pool 455 Thread-Safe Collection Classes 456 Summary 456 In Search of C# Canonical Forms 459 Reference Type Canonical Forms 459 Default to sealed Classes 460 Use the Non-Virtual Interface (NVI) Pattern 461 Is the Object Cloneable? 464 Is the Object Disposable? 470 Does the Object Need a Finalizer? 473 What Does Equality Mean for This Object? 480 Reference Types and Identity Equality 481 Value Equality 484 Overriding Object.Equals for Reference Types 484 If You Override Equals, Override GetHashCode Too 487 Does the Object Support Ordering? 491 Is the Object Formattable? 493 Is the Object Convertible? 497 Prefer Type Safety at All Times 499 Using Immutable Reference Types 503 Value Type Canonical Forms 506 Override Equals for Better Performance 507 Do Values of This Type Support Any Interfaces? 511 Implement Type-Safe Forms of Interface Members and Derived Methods 512 Summary 514 Checklist for Reference Types 515 Checklist for Value Types 516 Extension Methods 519 Introduction to Extension Methods 519 How Does the Compiler Find Extension Methods? 520 Under the Covers 523 Code Readability versus Code Understandability 524 Recommendations for Use 525 Consider Extension Methods Over Inheritance 525 Isolate Extension Methods in Separate Namespace 526 Changing a Type’s Contract Can Break Extension Methods 527 Transforms 527 Operation Chaining 532 Custom Iterators 533 Borrowing from Functional Programming 535 The Visitor Pattern 541 Summary 545 Lambda Expressions 547 Introduction to Lambda Expressions 547 Lambda Expressions and Closures 548 Closures in C# 1.0 551 Closures in C# 2.0 553 Lambda Statements 554 Expression Trees 554 Operating on Expressions 557 Functions as Data 558 Useful Applications of Lambda Expressions 559 Iterators and Generators Revisited 559 More on Closures (Variable Capture) and Memoization 563 Currying 568 Anonymous Recursion 570 Summary 571 LINQ: Language Integrated Query 573 A Bridge to Data 574 Query Expressions 574 Extension Methods and Lambda Expressions Revisited 576 Standard Query Operators 577 C# Query Keywords 579 The from Clause and Range Variables 579 The join Clause 580 The where Clause and Filters 582 The orderby Clause 583 The select Clause and Projection 584 The let Clause 586 The group Clause 587 The into Clause and Continuations 590 The Virtues of Being Lazy 592 C# Iterators Foster Laziness 592 Subverting Laziness 593 Executing Queries Immediately 595 Expression Trees Revisited 596 Techniques from Functional Programming 596 Custom Standard Query Operators and Lazy Evaluation 596 Replacing foreach Statements 605 Summary 606 Dynamic Types 607 What does dynamic Mean? 607 How Does dynamic Work? 610 The Great Unification 612 Call Sites 612 Objects with Custom Dynamic Behavior 615 Efficiency 617 Boxing with Dynamic 619 Dynamic Conversions 620 Implicit Dynamic Expressions Conversion 621 Dynamic Overload Resolution 622 Dynamic Inheritance 624 You Cannot Derive from dynamic 625 You Cannot Implement dynamic Interfaces 625 You Can Derive From Dynamic Base Types 627 Duck Typing in C# 629 Limitations of dynamic Types 632 ExpandoObject: Creating Objects Dynamically 632 Summary 637 Index 639 Symbols 639 A 639 B 640 C 640 D 643 E 644 F 645 G 645 H 646 I 646 J 648 K 648 L 648 M 649 N 650 O 650 P 651 Q 652 R 652 S 653 T 655 V 656 U 656 W 657 X 657 Y 657