Introduction xxxiii Part I: The C# Ec osystem Chapter 1: The C# Environment 3 Visual Studio 3 The C# Compiler 4 The CLR 6 The .NET Framework 8 Summary 9 Exercises 10 Chapter 2: Writing a First Program 11 Types of Projects 11 Console Applications 14 Windows Forms Applications 16 WPF Applications 19 Windows Store Applications 21 Summary 23 Exercises 24 Chapter 3: Program and Code File Structure 27 Hidden Files 28 Preprocessor Directives 31 #define and #undef 31 #if, #else, #elif, and #endif 33 #warning and #error 34 #line 34 #region and #endregion 35 #pragma 36 Code File Structure 37 The using Directive 38 The namespace Statement 40 Class Definitions 42 Comments 43 End-of-line and Multiline Comments 43 XML Comments 45 Summary 48 Exercises 48 Part II: C# Language Elements Chapter 4: Data Types, Variables, and Constants 53 Data Types 54 Value Versus Reference Types 57 The var Keyword 58 Variable Declaration Syntax 59 Name 62 Attributes 62 Accessibility 63 Static, Constant, and Volatile Variables 64 Initialization 65 Classes and Structures 66 Arrays 67 Collections 68 Literal Type Characters 69 Data Type Conversion 72 Implicit Conversion 73 Casting 74 Using the as Operator 76 Casting Arrays 77 Parsing 77 Using System.Convert 78 Using System.BitConverter 78 ToString 79 Scope 79 Block Scope 79 Method Scope 80 Class Scope 81 Restricting Scope 81 Parameter Declarations 82 By Value 82 By Reference 83 For Output 84 Unusual Circumstances and Exceptions 85 Properties 86 Enumerations 88 Nullable Types 92 Delegates 93 Summary 95 Exercises 96 Chapter 5: Operators 99 Arithmetic Operators 100 Result Data Type 100 Shift Operators 101 Increment and Decrement Operators 101 Comparison Operators 102 Logical Operators 103 Bitwise Operators 105 Conditional and Null-coalescing Operators 106 Assignment Operators 107 Operator Precedence 108 The StringBuilder Class 110 DateTime and TimeSpan Operations 111 Operator Overloading 112 Comparison Operators 113 Logical Operators 115 Type Conversion Operators 115 Summary 117 Exercises 118 Chapter 6: Methods 121 Method Declarations 122 Attributes 122 Accessibility 124 Modifiers 124 Name 128 Return Type 128 Parameters 129 Implementing Interfaces 133 Extension Methods 135 Lambda Expressions 136 Expression Lambdas 136 Statement Lambdas 137 Async Lambdas 138 Variance 139 Asynchronous Methods 140 Calling EndInvoke Directly 140 Handling a Callback 141 Using Async and Await 144 Summary 146 Exercises 147 Chapter 7: Program Control Statements 151 Decision Statements 151 if-else Statements 152 switch Statements 153 Enumerated Values 156 Conditional and Null-coalescing Operators 157 Looping Statements 157 for Loops 157 Noninteger for Loops 159 while Loops 160 do Loops 161 foreach Loops 161 Enumerators 163 Iterators 164 break Statements 165 continue Statements 165 Summary 166 Exercises 166 Chapter 8: LINQ 169 Introduction to LINQ 171 Basic LINQ Query Syntax 173 from 173 where 174 orderby 175 select 175 Using LINQ Results 177 Advanced LINQ Query Syntax 178 join 178 join into 179 group by 179 Aggregate Values 181 Set Methods 182 Limiting Results 183 Other LINQ Methods 184 LINQ Extension Methods 185 Method-Based Queries 185 Method-Based Queries with Lambda Functions 187 Extending LINQ 188 LINQ to Objects 189 LINQ to XML 189 XML Literals 190 LINQ into XML 191 LINQ out of XML 192 LINQ to ADO.NET 194 LINQ to SQL and LINQ to Entities 194 LINQ to DataSet 195 PLINQ 198 Summary 200 Exercises 201 Chapter 9: Error Handling 205 Bugs Versus Undesirable Conditions 206 Catching Bugs 206 Code Contracts 209 Catching Undesirable Conditions 213 Global Exception Handling 216 try catch Blocks 220 Exception Objects 223 Throwing Exceptions 224 Rethrowing Exceptions 226 Custom Exceptions 227 Summary 229 Exercises 229 Chapter 10: Tracing and Debugging 231 The Debug Menu 232 The Debug ? Windows Submenu 234 The Breakpoints Window 235 The Immediate Window 237 Trace Listeners 238 Summary 240 Exercises 241 Part III: Object-Oriented Programming Chapter 11: OO P Concepts 245 Classes 245 Encapsulation 248 Inheritance 250 Inheritance Hierarchies 251 Refinement and Abstraction 252 Has-a and Is-a Relationships 257 Adding and Modifying Class Features 257 Hiding and Overriding 258 abstract 261 sealed 262 Polymorphism 263 Summary 266 Exercises 266 Chapter 12: Classes and Structures 269 Classes 270 attributes 270 accessibility 271 abstract sealed static 272 partial 273 Structures 275 Value Versus Reference Types 275 Memory Requirements 276 Heap and Stack Performance 277 Object Assignment 277 Parameter Passing 277 Boxing and Unboxing 281 Constructors 282 Structure Instantiation Details 285 Garbage Collection 286 Destructors 286 Dispose 288 Events 290 Declaring Events 290 Raising Events 292 Catching Events 292 Using Event Delegate Types 292 Using Static Events 296 Hiding and Overriding Events 296 Raising Parent Class Events 296 Implementing Custom Events 297 Static Methods 298 Summary 300 Exercises 301 Chapter 13: Namespaces 303 Collisions in .NET 304 The using Directive 304 Project Templates 307 Item Templates 309 The Default Namespace 311 Making Namespaces 311 Resolving Namespaces 313 The global Namespace 314 Summary 315 Exercises 315 Chapter 14: Collection Classes 317 Arrays 318 Dimensions 318 Lower Bounds 318 Resizing 319 Speed 320 Other Array Class Features 320 System.Collections 321 ArrayList 321 StringCollection 324 NameValueCollection 325 Dictionaries 326 ListDictionary 327 Hashtable 328 HybridDictionary 329 StringDictionary 329 SortedList 329 CollectionsUtil 331 Stacks and Queues 331 Stack 331 Queue 333 Generic Collections 335 Collection Initializers 337 Iterators 338 Summary 339 Exercises 340 Chapter 15: Generics 343 Advantages of Generics 344 Defining Generics 344 Generic Constructors 345 Multiple Types 346 Constrained Types 348 Default Values 352 Instantiating Generic Classes 352 Generic Collection Classes 352 Generic Methods 352 Generics and Extension Methods 353 Summary 354 Exercises 355 Part IV: Interacting with the Environment Chapter 16: Printing 359 Windows Forms Printing 359 Basic Printing 360 Drawing Basics 365 WPF Printing 380 Using a Paginator 381 Creating Documents 385 Summary 390 Exercises 390 Chapter 17: Configuration and Resources 393 Environment Variables 394 Setting Environment Variables 394 Using System.Environment 395 Registry 397 Configuration Files 402 Resource Files 405 Application Resources 405 Embedded Resources 406 Localization Resources 407 Summary 408 Exercises 408 Chapter 18: Streams 411 Stream 412 FileStream 414 MemoryStream 415 BinaryReader and BinaryWriter 416 TextReader and TextWriter 418 StringReader and StringWriter 419 StreamReader and StreamWriter 421 Exists, OpenText, CreateText, and AppendText 422 Custom Stream Classes 423 Summary 423 Exercises 424 Chapter 19: File System Objects 425 Filesystem Permissions 426 .
NET Framework Classes 426 Directory 426 File 428 DriveInfo 430 DirectoryInfo 431 FileInfo 432 FileSystemWatcher 434 Path 436 Using the Recycle Bin 438 Using the FileIO.FileSystem Class 438 Using API Functions 439 Using Shell32.Shell 440 Summary 443 Exercises 444 Chapter 20: Networking 445 Networking Classes 446 Downloading Information 448 Downloading with WebClient 448 Downloading with WebRequest 451 Uploading Information 455 Uploading with WebClient 455 Uploading with WebRequest 455 Getting FTP Information 456 Sending E?mail 458 Sending Text Messages 460 Summary 462 Exercises 463