Copyrighted Material
Total Page:16
File Type:pdf, Size:1020Kb
Contents Acknowledgments xiii Introduction xlvii Part I: The C# Language 1 Chapter 1: .NET Architecture 3 The Relationship of C# to .NET 4 The Common Language Runtime 4 Platform Independence 4 Performance Improvement 4 Language Interoperability 5 A Closer Look at Intermediate Language 7 Support for Object Orientation and Interfaces 7 Distinct Value and Reference Types 8 Strong Data Typing 8 Error Handling with Exceptions 14 Use of Attributes 15 Assemblies 15 Private Assemblies 16 Shared Assemblies 16 Reflection 17 .NET Framework Classes 17 Namespaces 18 Creating .NET Applications Using C# 19 Creating ASP .NET Applications 19 Creating WindowsCOPYRIGHTED Forms MATERIAL 21 Using the Windows Presentation Foundation (WPF) 21 Windows Controls 21 Windows Services 21 Windows Communication Foundation (WCF) 22 The Role of C# in the .NET Enterprise Architecture 22 Summary 23 fftoc.inddtoc.indd xxvv 22/19/08/19/08 88:30:27:30:27 PPMM Contents Chapter 2: C# Basics 25 Before We Start 25 Your First C# Program 26 The Code 26 Compiling and Running the Program 26 A Closer Look 27 Variables 29 Initialization of Variables 29 Type Inference 30 Variable Scope 31 Constants 34 Predefined Data Types 34 Value Types and Reference Types 35 CTS Types 36 Predefined Value Types 36 Predefined Reference Types 40 Flow Control 42 Conditional Statements 42 Loops 45 Jump Statements 49 Enumerations 50 Arrays 51 Namespaces 52 The using Directive 53 Namespace Aliases 54 The Main() Method 55 Multiple Main() Methods 55 Passing Arguments to Main() 56 More on Compiling C# Files 57 Console I/O 58 Using Comments 60 Internal Comments within the Source Files 60 XML Documentation 61 The C# Preprocessor Directives 63 #define and #undef 63 #if, #elif, #else, and #endif 64 #warning and #error 65 #region and #endregion 65 #line 65 #pragma 66 C# Programming Guidelines 66 xvi fftoc.inddtoc.indd xxvivi 22/19/08/19/08 88:30:28:30:28 PPMM Contents Rules for Identifiers 66 Usage Conventions 67 Summary 73 Chapter 3: Objects and Types 75 Classes and Structs 76 Class Members 76 Data Members 77 Function Members 77 Anonymous Types 91 Structs 92 Structs Are Value Types 93 Structs and Inheritance 94 Constructors for Structs 94 Partial Classes 95 Static Classes 96 The Object Class 97 System.Object Methods 97 The ToString() Method 98 Extension Methods 99 Summary 100 Chapter 4: Inheritance 101 Types of Inheritance 101 Implementation versus Interface Inheritance 101 Multiple Inheritance 102 Structs and Classes 102 Implementation Inheritance 103 Virtual Methods 104 Hiding Methods 104 Calling Base Versions of Functions 106 Abstract Classes and Functions 106 Sealed Classes and Methods 107 Constructors of Derived Classes 107 Modifiers 112 Visibility Modifiers 112 Other Modifiers 113 Interfaces 114 Defining and Implementing Interfaces 115 Derived Interfaces 118 Summary 120 xvii fftoc.inddtoc.indd xxviivii 22/19/08/19/08 88:30:29:30:29 PPMM Contents Chapter 5: Arrays 121 Simple Arrays 121 Array Declaration 121 Array Initialization 122 Accessing Array Elements 123 Using Reference Types 123 Multidimensional Arrays 125 Jagged Arrays 126 Array Class 127 Properties 127 Creating Arrays 127 Copying Arrays 128 Sorting 129 Array and Collection Interfaces 132 IEnumerable 132 ICollection 132 IList 132 Enumerations 133 IEnumerator Interface 134 foreach Statement 134 yield Statement 134 Summary 139 Chapter 6: Operators and Casts 141 Operators 141 Operator Shortcuts 143 The Conditional Operator 144 The checked and unchecked Operators 145 The is Operator 146 The as Operator 146 The sizeof Operator 146 The typeof Operator 146 Nullable Types and Operators 147 The Null Coalescing Operator 147 Operator Precedence 147 Type Safety 148 Type Conversions 149 Boxing and Unboxing 152 Comparing Objects for Equality 153 Comparing Reference Types for Equality 153 Comparing Value Types for Equality 154 xviii fftoc.inddtoc.indd xxviiiviii 22/19/08/19/08 88:30:29:30:29 PPMM Contents Operator Overloading 155 How Operators Work 156 Operator Overloading Example: The Vector Struct 157 Which Operators Can You Overload? 163 User-Defined Casts 164 Implementing User-Defined Casts 165 Multiple Casting 171 Summary 175 Chapter 7: Delegates and Events 177 Delegates 177 Declaring Delegates in C# 178 Using Delegates in C# 179 Simple Delegate Example 182 BubbleSorter Example 184 Multicast Delegates 187 Anonymous Methods 190 Lambda Expressions 191 Covariance and Contra-variance 193 Events 194 The Receiver’s View of Events 195 Defining Events 197 Summary 201 Chapter 8: Strings and Regular Expressions 203 System.String 204 Building Strings 205 StringBuilder Members 208 Format Strings 209 Regular Expressions 214 Introduction to Regular Expressions 215 The RegularExpressionsPlayground Example 216 Displaying Results 219 Matches, Groups, and Captures 220 Summary 222 Chapter 9: Generics 223 Overview 223 Performance 224 Type Safety 225 xix fftoc.inddtoc.indd xxixix 22/19/08/19/08 88:30:30:30:30 PPMM Contents Binary Code Reuse 225 Code Bloat 226 Naming Guidelines 226 Creating Generic Classes 226 Generic Classes’ Features 231 Default Values 231 Constraints 232 Inheritance 234 Static Members 235 Generic Interfaces 235 Generic Methods 236 Generic Delegates 238 Implementing Methods Called by Delegates 238 Using Generic Delegates with the Array Class 240 Other Generic Framework Types 243 Nullable<T> 243 EventHandler<TEventArgs> 244 ArraySegment<T> 245 Summary 246 Chapter 10: Collections 247 Collection Interfaces and Types 247 Lists 250 Creating Lists 252 Read-Only Collections 261 Queues 261 Stacks 266 Linked Lists 268 Sorted Lists 275 Dictionaries 278 Key Type 278 Dictionary Example 280 Lookup 283 Other Dictionary Classes 284 HashSet 286 Bit Arrays 289 BitArray 289 BitVector32 291 Performance 294 Summary 296 xx fftoc.inddtoc.indd xxxx 22/19/08/19/08 88:30:30:30:30 PPMM Contents Chapter 11: Language Integrated Query 297 LINQ Overview 297 Query using List<T> 298 Extension Methods 304 Lambda Expressions 306 LINQ Query 307 Deferred Query Execution 307 Standard Query Operators 309 Filtering 311 Filtering with Index 312 Type Filtering 312 Compound from 313 Sorting 314 Grouping 315 Grouping with Nested Objects 316 Join 317 Set Operations 318 Partitioning 319 Aggregate Operators 321 Conversion 322 Generation Operators 323 Expression Trees 324 LINQ Providers 327 Summary 328 Chapter 12: Memory Management and Pointers 329 Memory Management Under the Hood 329 Value Data Types 330 Reference Data Types 331 Garbage Collection 333 Freeing Unmanaged Resources 334 Destructors 335 The IDisposable Interface 336 Implementing IDisposable and a Destructor 337 Unsafe Code 339 Accessing Memory Directly with Pointers 339 Pointer Example: PointerPlayaround 347 Using Pointers to Optimize Performance 352 Summary 355 xxi fftoc.inddtoc.indd xxxixi 22/19/08/19/08 88:30:30:30:30 PPMM Contents Chapter 13: Reflection 357 Custom Attributes 358 Writing Custom Attributes 358 Custom Attribute Example: WhatsNewAttributes 362 Reflection 365 The System.Type Class 365 The TypeView Example 367 The Assembly Class 370 Completing the WhatsNewAttributes Example 371 Summary 376 Chapter 14: Errors and Exceptions 377 Exception Classes 378 Catching Exceptions 379 Implementing Multiple Catch Blocks 382 Catching Exceptions from Other Code 385 System.Exception Properties 386 What Happens If an Exception Isn’t Handled? 386 Nested try Blocks 387 User-Defined Exception Classes 389 Catching the User-Defined Exceptions 390 Throwing the User-Defined Exceptions 391 Defining the User-Defined Exception Classes 394 Summary 397 Part II: Visual Studio 399 Chapter 15: Visual Studio 2008 401 Working with Visual Studio 2008 401 Creating a Project 406 Solutions and Projects 412 Windows Application Code 415 Reading in Visual Studio 6 Projects 416 Exploring and Coding a Project 416 Building a Project 427 Debugging 430 Refactoring 434 Multi-Targeting 436 WPF, WCF, WF, and More 438 xxii fftoc.inddtoc.indd xxxiixii 22/19/08/19/08 88:30:31:30:31 PPMM Contents Building WPF Applications in Visual Studio 438 Building WF Applications in Visual Studio 439 Summary 441 Chapter 16: Deployment 443 Designing for Deployment 443 Deployment Options 444 Xcopy 444 Copy Web Tool 444 Publishing Web Sites 444 Deployment Projects 444 ClickOnce 444 Deployment Requirements 444 Deploying the .NET Runtime 446 Simple Deployment 446 Xcopy 447 Xcopy and Web Applications 447 Copy Web Tool 447 Publishing a Web Site 447 Installer Projects 448 What Is Windows Installer? 449 Creating Installers 449 ClickOnce 458 ClickOnce Operation 458 Publishing an Application 459 ClickOnce Settings 459 Application Cache 460 Security 460 Advanced Options 460 Summary 466 Part III: Base Class Libraries 467 Chapter 17: Assemblies 469 What Are Assemblies? 469 Features of Assemblies 470 Assembly Structure 470 Assembly Manifests 472 Namespaces, Assemblies, and Components 472 Private and Shared Assemblies 472 xxiii fftoc.inddtoc.indd xxxiiixiii 22/19/08/19/08 88:30:31:30:31 PPMM Contents Satellite Assemblies 473 Viewing Assemblies 473 Creating Assemblies 474 Creating Modules and Assemblies 474 Assembly Attributes 475 Dynamic Loading and Creating Assemblies 478 Application Domains 481 Shared Assemblies 485 Strong Names 486 Integrity Using Strong Names 486 Global Assembly Cache 487 Creating a Shared Assembly 489 Create a Strong Name 489 Install the Shared Assembly 491 Using the Shared Assembly 491 Delayed Signing of Assemblies 492 References 493 Native Image Generator 494 Configuring .NET Applications 495 Configuration Categories 496 Configuring Directories for Assembly Searches 497