Introduction xxvii Part I: The C# Language Chapter 1: Introducing C# 3 What Is .NET? 3 .NET Framework, .NET Standard, and .NET Core 4 Writing Programs Using .NET 5 CIL and JIT 5 Assemblies 6 Managed Code 7 Garbage Collection 7 Fitting It Together 7 Linking 8 What Is C#? 8 Applications You Can Write with C# 9 C# in This Book 10 Visual Studio 10 Visual Studio Products 10 Solutions 11 Chapter 2: Writing a C# Program 13 The Visual Studio Development Environment 14 Console Applications 18 The Solution Explorer 21 The Properties Window 22 The Error List Window 23 Desktop Applications 23 Chapter 3: Variables and Expressions 29 Basic C# Syntax 30 Basic C# Console Application Structure 33 Variables 34 Simple Types 34 Variable Naming 39 Literal Values 39 Binary Literals and Digit Separators 40 String Literals 41 Expressions 42 Mathematical Operators 43 Assignment Operators 48 Operator Precedence 49 Namespaces 50 Chapter 4: Flow Control 55 Boolean Logic 56 Boolean Bitwise and Assignment Operators 58 Operator Precedence Updated 60 Branching 60 The Ternary Operator 61 The if Statement 61 Checking More Conditions Using if Statements 64 The switch Statement 65 Looping 68 do Loops 68 while Loops 71 for Loops 73 Interrupting Loops 74 Infinite Loops 75 Chapter 5: More About Variables 79 Type Conversion 80 Implicit Conversions 80 Explicit Conversions 82 Explicit Conversions Using the Convert Commands 84 Complex Variable Types 87 Enumerations 87 Defining Enumerations 88 Structs 91 Defining Structs 92 Arrays 94 Declaring Arrays 95 foreach Loops 98 Pattern Matching with switch case Expression 98 Multidimensional Arrays 102 Arrays of Arrays 104 String Manipulation 105 Chapter 6: Functions 113 Defining and Using Functions 114 Return Values 117 Parameters 118 Parameter Matching 121 Parameter Arrays 121 Reference and Value Parameters 123 Out Parameters 125 Tuples 126 Variable Scope 128 Variable Scope in Other Structures 131 Parameters and Return Values versus Global Data 132 Local Functions 134 The Main() Function 135 Struct Functions 137 Overloading Functions 138 Using Delegates 140 Chapter 7: Debugging and Error Handling 145 Debugging in Visual Studio 146 Debugging in Nonbreak (Normal) Mode 147 Outputting Debugging Information 148 Tracepoints 152 Diagnostics Output versus Tracepoints 154 Debugging in Break Mode 155 Entering Break Mode 155 Monitoring Variable Content 158 Stepping through Code 161 Immediate and Command Windows 162 The Call Stack Window 163 Error Handling 163 try catch finally 164 Throw Expressions 172 Listing and Configuring Exceptions 172 Chapter 8: Introduction to Object-oriented Programming 175 What Is Object-Oriented Programming? 176 What Is an Object? 177 Properties and Fields 178 Methods 179 Everything''s an Object 180 The Life Cycle of an Object 180 Constructors 180 Destructors 181 Static and Instance Class Members 181 Static Constructors 181 Static Classes 182 OOP Techniques 182 Interfaces 182 Disposable Objects 184 Inheritance 184 Polymorphism 187 Interface Polymorphism 188 Relationships between Objects 189 Containment 189 Collections 190 Operator Overloading 191 Events 191 Reference Types versus Value Types 192 OOP in Desktop Applications 192 Chapter 9: Defining Classes 199 Class Definitions in C# 200 Interface Definitions 202 System.Object 205 Constructors and Destructors 207 Constructor Execution Sequence 209 OOP Tools in Visual Studio 212 The Class View Window 212 The Object Browser 214 Adding Classes 216 Class Diagrams 217 Class Library Projects 219 Interfaces versus Abstract Classes 223 Struct Types 225 Shallow Copying versus Deep Copying 227 Chapter 10: Defining Class Members 231 Member Definitions 232 Defining Fields 232 Defining Methods 233 Defining Properties 234 Tuple Deconstruction 239 Refactoring Members 240 Automatic Properties 241 Additional Class Member Topics 242 Hiding Base Class Methods 242 Calling Overridden or Hidden Base Class Methods 244 The this Keyword 244 Using Nested Type Definitions 245 Interface Implementation 247 Implementing Interfaces in Classes 248 Explicit Interface Member Implementation 249 Additional Property Accessors 249 Partial Class Definitions 250 Partial Method Definitions 251 Example Application 252 Planning the Application 253 The Card Class 253 The Deck Class 253 Writing the Class Library 253 Adding the Suit and Rank Enumerations 254 Adding the Card Class 256 Adding the Deck Class 258 A Client Application for the Class Library 261 The Call Hierarchy Window 262 Chapter 11: Collections, Comparisons, and Conversions 265 Collections 266 Using Collections 267 Defining Collections 272 Indexers 273 Adding a CardCollection to CardLib 275 Keyed Collections and IDictionary 278 Iterators 279 Iterators and Collections 284 Deep Copying 285 Adding Deep Copying to CardLib 286 Comparisons 288 Type Comparisons 288 Boxing and Unboxing 289 The is Operator 290 Pattern Matching with the is Operator Pattern Expression 293 Value Comparisons 294 Operator Overloading 294 Adding Operator Overloads to CardLib 299 The IComparable and IComparer Interfaces 304 Sorting Collections 306 Conversions 309 Overloading Conversion Operators 310 The as Operator 311 Chapter 12: Generics 315 What Are Generics? 316 Using Generics 317 Nullable Types 317 Operators and Nullable Types 318 The ?? Operator 319 The ?. Operator 320 Working with Nullable Types 321 The System.
Collections.Generic Namespace 325 List 326 Sorting and Searching Generic Lists 327 Dictionary 333 Modifying CardLib to Use a Generic Collection Class 334 Defining Generic Types 335 Defining Generic Classes 336 The default Keyword 338 Constraining Types 338 Inheriting from Generic Classes 344 Generic Operators 345 Generic Structs 346 Defining Generic Interfaces 346 Defining Generic Methods 346 Defining Generic Delegates 348 Variance 348 Covariance 349 Contravariance 350 Chapter 13: Additional C# Techniques 355 The :: Operator and the Global Namespace Qualifier 356 Custom Exceptions 357 Adding Custom Exceptions to CardLib 358 Events 359 What Is an Event? 359 Handling Events 361 Defining Events 363 Multipurpose Event Handlers 367 The EventHandler and Generic EventHandler Types 370 Return Values and Event Handlers 370 Anonymous Methods 370 Expanding and Using CardLib 371 Attributes 380 Reading Attributes 380 Creating Attributes 381 Initializers 382 Object Initializers 383 Collection Initializers 385 Type Inference 388 Anonymous Types 390 Dynamic Lookup 394 The dynamic Type 395 Advanced Method Parameters 399 Optional Parameters 399 Optional Parameter Values 400 The OptionalAttribute Attribute 400 Optional Parameter Order 401 Named Parameters 401 Lambda Expressions 405 Anonymous Methods Recap 405 Lambda Expressions for Anonymous Methods 407 Lambda Expression Parameters 410 Lambda Expression Statement Bodies 411 Lambda Expressions as Delegates and Expression Trees 412 Lambda Expressions and Collections 413 Part II: Data Access Chapter 14: Files 423 File Classes for Input and Output 424 The File and Directory Classes 424 The FileInfo Class 426 The DirectoryInfo Class 428 Path Names and Relative Paths 428 Streams 429 Classes for Using Streams 429 The FileStream Object 429 File Position 430 Reading Data 432 Writing Data 434 The StreamWriter Object 436 The StreamReader Object 439 Reading Data 440 Asynchronous File Access 441 Reading and Writing Compressed Files 442 Monitoring the File System 445 Chapter 15: XML and JSON 453 XML Basics 454 JSON Basics 455 XML Schemas 455 XML Document Object Model 458 The XmlDocument Class 458 The XmlElement Class 459 Changing the Values of Nodes 463 Inserting New Nodes 465 Deleting Nodes 467 Selecting Nodes 469 Searching XML with XPath 469 JSON Serialization and Deserialization 473 Chapter 16: LINQ 479 LINQ to XML 480 LINQ to XML Functional Constructors 480 Working with XML Frag.