INTRODUCTION xxxi PART I: THE C# LANGUAGE CHAPTER 1: INTRODUCING C# 3 What Is the .NET Framework? 3 What''s in the .NET Framework? 4 Writing Applications Using the .NET Framework 4 CIL and JIT 4 Assemblies 5 Managed Code 5 Garbage Collection 6 Fitting It Together 6 Linking 7 What Is C#? 7 Applications You Can Write with C# 8 C# in this Book 8 Visual Studio 2012 8 Visual Studio Express 2012 Products 9 Solutions 9 Summary 10 CHAPTER 2: WRITING A C# PROGRAM 13 The Visual Studio 2012 Development Environment 14 Console Applications 16 The Solution Explorer 18 The Properties Window 19 The Error List Window 19 Desktop Applications 20 Summary 23 CHAPTER 3: VARIABLES AND EXPRESSIONS 25 Basic C# Syntax 26 Basic C# Console Application Structure 28 Variables 29 Simple Types 29 Variable Naming 33 Naming Conventions 34 Literal Values 34 Variable Declaration and Assignment 36 Expressions 37 Mathematical Operators 37 Assignment Operators 41 Operator Precedence 42 Namespaces 42 Summary 45 CHAPTER 4: FLOW CONTROL 49 Boolean Logic 49 Boolean Assignment Operators 52 Bitwise Operators 53 Operator Precedence Updated 57 The goto Statement 58 Branching 59 The Ternary Operator 59 The if Statement 59 Checking More Conditions Using if Statements 62 The switch Statement 63 Looping 65 do Loops 66 while Loops 68 for Loops 70 Interrupting Loops 74 Infi nite Loops 75 Summary 75 CHAPTER 5: MORE ABOUT VARIABLES 79 Type Conversion 80 Implicit Conversions 80 Explicit Conversions 81 Explicit Conversions Using the Convert Commands 84 Complex Variable Types 86 Enumerations 87 Defi ning Enumerations 87 Structs 90 Defi ning Structs 90 Arrays 92 Declaring Arrays 93 foreach Loops 95 Multidimensional Arrays 96 Arrays of Arrays 97 String Manipulation 98 Summary 102 CHAPTER 6: FUNCTIONS 105 Defi ning and Using Functions 106 Return Values 108 Parameters 109 Parameter Matching 111 Parameter Arrays 111 Reference and Value Parameters 113 Out Parameters 115 Variable Scope 116 Variable Scope in Other Structures 118 Parameters and Return Values versus Global Data 120 The Main( ) Function 121 Struct Functions 123 Overloading Functions 124 Using Delegates 125 Summary 128 CHAPTER 7: DEBUGGING AND ERROR HANDLING 131 Debugging in Visual Studio 132 Debugging in Nonbreak (Normal) Mode 132 Outputting Debugging Information 133 Tracepoints 137 Diagnostics Output Versus Tracepoints 139 Debugging in Break Mode 140 Entering Break Mode 140 Monitoring Variable Content 142 Stepping Through Code 144 Immediate and Command Windows 146 The Call Stack Window 146 Error Handling 147 try.catch.finally 147 Listing and Configuring Exceptions 152 Notes on Exception Handling 152 Summary 153 CHAPTER 8: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING 157 What Is Object-Oriented Programming? 158 What Is an Object? 158 Properties and Fields 159 Methods 160 Everything''s an Object 161 The Life Cycle of an Object 161 Constructors 161 Destructors 162 Static and Instance Class Members 162 Static Constructors 162 Static Classes 163 OOP Techniques 163 Interfaces 163 Disposable Objects 164 Inheritance 165 Polymorphism 167 Interface Polymorphism 168 Relationships Between Objects 168 Containment 168 Collections 169 Operator Overloading 169 Events 170 Reference Types Versus Value Types 170 OOP in Desktop Applications 171 Summary 174 CHAPTER 9: DEFINING CLASSES 177 Class Defi nitions in C# 177 Interface Defi nitions 180 System.Object 182 Constructors and Destructors 184 Constructor Execution Sequence 185 OOP Tools in Visual Studio 188 The Class View Window 188 The Object Browser 190 Adding Classes 191 Class Diagrams 192 Class Library Projects 193 Interfaces Versus Abstract Classes 196 Struct Types 198 Shallow Copying Versus Deep Copying 200 Summary 200 CHAPTER 10: DEFINING CLASS MEMBERS 203 Member Defi nitions 203 Defi ning Fields 204 Defi ning Methods 204 Defi ning Properties 205 Adding Members from a Class Diagram 210 Adding Methods 210 Adding Properties 212 Adding Fields 212 Refactoring Members 212 Automatic Properties 213 Additional Class Member Topics 213 Hiding Base Class Methods 213 Calling Overridden or Hidden Base Class Methods 215 The this Keyword 216 Using Nested Type Defi nitions 216 Interface Implementation 218 Implementing Interfaces in Classes 219 Explicit Interface Member Implementation 220 Additional Property Accessors 220 Partial Class Defi nitions 221 Partial Method Defi nitions 222 Example Application 224 Planning the Application 224 The Card Class 224 The Deck Class 224 Writing the Class Library 224 Adding the Suit and Rank Enumerations 225 Adding the Card Class 226 Adding the Deck Class 227 A Client Application for the Class Library 230 The Call Hierarchy Window 231 Summary 232 CHAPTER 11: COLLECTIONS, COMPARISONS, AND CONVERSIONS 235 Collections 236 Using Collections 236 Defi ning Collections 241 Indexers 242 Adding a Cards Collection to CardLib 244 Keyed Collections and IDictionary 247 Iterators 248 Iterators and Collections 252 Deep Copying 253 Adding Deep Copying to CardLib 255 Comparisons 256 Type Comparisons 256 Boxing and Unboxing 257 The is Operator 258 Value Comparisons 260 Operator Overloading 261 Adding Operator Overloads to CardLib 265 The IComparable and IComparer Interfaces 269 Sorting Collections 271 Conversions 274 Overloading Conversion Operators 274 The as Operator 275 Summary 276 CHAPTER 12: GENERICS 279 What Are Generics? 280 Using Generics 281 Nullable Types 281 Operators and Nullable Types 282 The ?? Operator 283 The System.Collections.
Generic Namespace 287 List 287 Sorting and Searching Generic Lists 289 Dictionary 294 Modifying CardLib to Use a Generic Collection Class 295 Defi ning Generic Types 296 Defi ning Generic Classes 296 The default Keyword 298 Constraining Types 298 Inheriting from Generic Classes 304 Generic Operators 305 Generic Structs 306 Defi ning Generic Interfaces 306 Defi ning Generic Methods 306 Defi ning Generic Delegates 308 Variance 308 Covariance 309 Contravariance 310 Summary 310 CHAPTER 13: ADDITIONAL OOP TECHNIQUES 313 The :: Operator and the Global Namespace Qualifi er 313 Custom Exceptions 315 Adding Custom Exceptions to CardLib 315 Events 316 What Is an Event? 316 Handling Events 317 Defi ning Events 319 Multipurpose Event Handlers 323 The EventHandler and Generic EventHandler Types 325 Return Values and Event Handlers 326 Anonymous Methods 326 Expanding and Using CardLib 327 A Card Game Client for CardLib 327 Attributes 334 Reading Attributes 334 Creating Attributes 335 Summary 336 CHAPTER 14: C# LANGUAGE ENHANCEMENTS 339 Initializers 340 Object Initializers 340 Collection Initializers 342 Type Inference 344 Anonymous Types 346 Dynamic Lookup 350 The dynamic Type 350 IDynamicMetaObjectProvider 354 Advanced Method Parameters 354 Optional Parameters 354 Optional Parameter Values 355 The OptionalAttribute Attribute 356 Optional Parameter Order 356 Named Parameters 356 Named and Optional Parameter Guidelines 360 Extension Methods 360 Lambda Expressions 364 Anonymous Methods Recap 364 Lambda Expressions for Anonymous Methods 365 Lambda Expression Parameters 368 Lambda Expression Statement Bodies 368 Lambda Expressions as Delegates and Expression Trees 369 Lambda Expressions and Collections 370 Caller Information Attributes 372 Summary 375 PART II: WINDOWS PROGRAMMING CHAPTER 15: BASIC DESKTOP PROGRAMMING 381 XAML 382 Separation of Concerns 382 XAML in Action 383 Namespaces 383 Code-Behind Files 384 The Playground 384 WPF Controls 385 Properties 386 Dependency Properties 387 Attached Properties 388 Events 388 Handling Events 389 Routed Events 390 Routed Commands 390 Control Types 392 Control Layout 393 Stack Order 393 Alignment, Margins, Padding, and Dimensions 393 Border 394 Canvas 394 DockPanel 395 StackPanel 397 WrapPanel 398<.