You are previewing Accelerated C# 2010.
O'Reilly logo
Accelerated C# 2010

Book Description

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

Table of Contents

  1. Copyright
  2. About the Author
  3. About the Technical Reviewer
  4. Acknowledgments
  5. Preface
    1. About This Book
  6. 1. C# Preview
    1. 1.1. Differences Between C# and C++
      1. 1.1.1. C#
      2. 1.1.2. C++
      3. 1.1.3. CLR Garbage Collection
    2. 1.2. Example of a C# Program
    3. 1.3. Overview of Features Added in C# 2.0
    4. 1.4. Overview of Features Added in C# 3.0
    5. 1.5. Overview of New C# 4.0 Features
    6. 1.6. Summary
  7. 2. C# and the CLR
    1. 2.1. The JIT Compiler in the CLR
    2. 2.2. Assemblies and the Assembly Loader
      1. 2.2.1. Minimizing the Working Set of the Application
      2. 2.2.2. Naming Assemblies
      3. 2.2.3. Loading Assemblies
    3. 2.3. Metadata
    4. 2.4. Cross-Language Compatibility
    5. 2.5. Summary
  8. 3. C# Syntax Overview
    1. 3.1. C# Is a Strongly Typed Language
    2. 3.2. Expressions
    3. 3.3. Statements and Expressions
    4. 3.4. Types and Variables
      1. 3.4.1. Value Types
        1. 3.4.1.1. Enumerations
        2. 3.4.1.2. Flags Enumerations
      2. 3.4.2. Reference Types
      3. 3.4.3. Default Variable Initialization
      4. 3.4.4. Implicitly Typed Local Variables
      5. 3.4.5. Type Conversion
        1. 3.4.5.1. Array Covariance
        2. 3.4.5.2. Boxing Conversion
      6. 3.4.6. as and is Operators
      7. 3.4.7. Generics
    5. 3.5. Namespaces
      1. 3.5.1. Defining Namespaces
      2. 3.5.2. Using Namespaces
    6. 3.6. Control Flow
      1. 3.6.1. if-else, while, do-while, and for
      2. 3.6.2. switch
      3. 3.6.3. foreach
      4. 3.6.4. break, continue, goto, return, and throw
    7. 3.7. Summary
  9. 4. Classes, Structs, and Objects
    1. 4.1. Class Definitions
      1. 4.1.1. Fields
      2. 4.1.2. Constructors
      3. 4.1.3. Methods
        1. 4.1.3.1. Static Methods
        2. 4.1.3.2. Instance Methods
      4. 4.1.4. Properties
        1. 4.1.4.1. Declaring Properties
        2. 4.1.4.2. Accessors
        3. 4.1.4.3. Read-Only and Write-Only Properties
        4. 4.1.4.4. Auto-Implemented Properties
      5. 4.1.5. Encapsulation
      6. 4.1.6. Accessibility
      7. 4.1.7. Interfaces
      8. 4.1.8. Inheritance
        1. 4.1.8.1. Accessibility of Members
        2. 4.1.8.2. Implicit Conversion and a Taste of Polymorphism
        3. 4.1.8.3. Member Hiding
        4. 4.1.8.4. The base Keyword
      9. 4.1.9. sealed Classes
      10. 4.1.10. abstract Classes
      11. 4.1.11. Nested Classes
      12. 4.1.12. Indexers
      13. 4.1.13. partial Classes
      14. 4.1.14. partial Methods
      15. 4.1.15. Static Classes
      16. 4.1.16. Reserved Member Names
        1. 4.1.16.1. Reserved Names for Properties
        2. 4.1.16.2. Reserved Names for Indexers
        3. 4.1.16.3. Reserved Names for Destructors
        4. 4.1.16.4. Reserved Names for Events
    2. 4.2. Value Type Definitions
      1. 4.2.1. Constructors
      2. 4.2.2. The Meaning of this
      3. 4.2.3. Finalizers
      4. 4.2.4. Interfaces
    3. 4.3. Anonymous Types
    4. 4.4. Object Initializers
    5. 4.5. Boxing and Unboxing
      1. 4.5.1. When Boxing Occurs
      2. 4.5.2. Efficiency and Confusion
    6. 4.6. System.Object
      1. 4.6.1. Equality and What It Means
      2. 4.6.2. The IComparable Interface
    7. 4.7. Creating Objects
      1. 4.7.1. The new Keyword
        1. 4.7.1.1. Using new with Value Types
        2. 4.7.1.2. Using new with Class Types
      2. 4.7.2. Field Initialization
      3. 4.7.3. Static (Class) Constructors
      4. 4.7.4. Instance Constructor and Creation Ordering
    8. 4.8. Destroying Objects
      1. 4.8.1. Finalizers
      2. 4.8.2. Deterministic Destruction
      3. 4.8.3. Exception Handling
    9. 4.9. Disposable Objects
      1. 4.9.1. The IDisposable Interface
      2. 4.9.2. The using Keyword
    10. 4.10. Method Parameter Types
      1. 4.10.1. Value Arguments
      2. 4.10.2. ref Arguments
      3. 4.10.3. out Parameters
      4. 4.10.4. param Arrays
      5. 4.10.5. Method Overloading
      6. 4.10.6. Optional Arguments
      7. 4.10.7. Named Arguments
    11. 4.11. Inheritance and Virtual Methods
      1. 4.11.1. Virtual and Abstract Methods
      2. 4.11.2. override and new Methods
      3. 4.11.3. sealed Methods
      4. 4.11.4. A Final Few Words on C# Virtual Methods
    12. 4.12. Inheritance, Containment, and Delegation
      1. 4.12.1. Choosing Between Interface and Class Inheritance
      2. 4.12.2. Delegation and Composition vs. Inheritance
    13. 4.13. Summary
  10. 5. Interfaces and Contracts
    1. 5.1. Interfaces Define Types
    2. 5.2. Defining Interfaces
      1. 5.2.1. What Can Be in an Interface?
      2. 5.2.2. Interface Inheritance and Member Hiding
    3. 5.3. Implementing Interfaces
      1. 5.3.1. Implicit Interface Implementation
      2. 5.3.2. Explicit Interface Implementation
      3. 5.3.3. Overriding Interface Implementations in Derived Classes
      4. 5.3.4. Beware of Side Effects of Value Types Implementing Interfaces
    4. 5.4. Interface Member Matching Rules
    5. 5.5. Explicit Interface Implementation with Value Types
    6. 5.6. Versioning Considerations
    7. 5.7. Contracts
      1. 5.7.1. Contracts Implemented with Classes
      2. 5.7.2. Interface Contracts
    8. 5.8. Choosing Between Interfaces and Classes
    9. 5.9. Summary
  11. 6. Overloading Operators
    1. 6.1. Just Because You Can Doesn't Mean You Should
    2. 6.2. Types and Formats of Overloaded Operators
    3. 6.3. Operators Shouldn't Mutate Their Operands
    4. 6.4. Does Parameter Order Matter?
    5. 6.5. Overloading the Addition Operator
    6. 6.6. Operators That Can Be Overloaded
      1. 6.6.1. Comparison Operators
      2. 6.6.2. Conversion Operators
      3. 6.6.3. Boolean Operators
    7. 6.7. Summary
  12. 7. Exception Handling and Exception Safety
    1. 7.1. How the CLR Treats Exceptions
    2. 7.2. Mechanics of Handling Exceptions in C#
      1. 7.2.1. Throwing Exceptions
      2. 7.2.2. Changes with Unhandled Exceptions Starting with .NET 2.0
      3. 7.2.3. Syntax Overview of the try, catch, and finally Statements
      4. 7.2.4. Rethrowing Exceptions and Translating Exceptions
      5. 7.2.5. Exceptions Thrown in finally Blocks
      6. 7.2.6. Exceptions Thrown in Finalizers
      7. 7.2.7. Exceptions Thrown in Static Constructors
    3. 7.3. Who Should Handle Exceptions?
    4. 7.4. Avoid Using Exceptions to Control Flow
    5. 7.5. Achieving Exception Neutrality
      1. 7.5.1. Basic Structure of Exception-Neutral Code
      2. 7.5.2. Constrained Execution Regions
      3. 7.5.3. Critical Finalizers and SafeHandle
    6. 7.6. Creating Custom Exception Classes
    7. 7.7. Working with Allocated Resources and Exceptions
    8. 7.8. Providing Rollback Behavior
    9. 7.9. Summary
  13. 8. Working with Strings
    1. 8.1. String Overview
    2. 8.2. String Literals
    3. 8.3. Format Specifiers and Globalization
      1. 8.3.1. Object.ToString, IFormattable, and CultureInfo
      2. 8.3.2. Creating and Registering Custom CultureInfo Types
      3. 8.3.3. Format Strings
      4. 8.3.4. Console.WriteLine and String.Format
      5. 8.3.5. Examples of String Formatting in Custom Types
      6. 8.3.6. ICustomFormatter
      7. 8.3.7. Comparing Strings
    4. 8.4. Working with Strings from Outside Sources
    5. 8.5. StringBuilder
    6. 8.6. Searching Strings with Regular Expressions
      1. 8.6.1. Searching with Regular Expressions
      2. 8.6.2. Searching and Grouping
      3. 8.6.3. Replacing Text with Regex
      4. 8.6.4. Regex Creation Options
    7. 8.7. Summary
  14. 9. Arrays, Collection Types, and Iterators
    1. 9.1. Introduction to Arrays
      1. 9.1.1. Implicitly Typed Arrays
      2. 9.1.2. Type Convertibility and Covariance
      3. 9.1.3. Sortability and Searchability
      4. 9.1.4. Synchronization
      5. 9.1.5. Vectors vs. Arrays
    2. 9.2. Multidimensional Rectangular Arrays
    3. 9.3. Multidimensional Jagged Arrays
    4. 9.4. Collection Types
      1. 9.4.1. Comparing ICollection<T> with ICollection
      2. 9.4.2. Collection Synchronization
      3. 9.4.3. Lists
      4. 9.4.4. Dictionaries
      5. 9.4.5. Sets
      6. 9.4.6. System.Collections.ObjectModel
      7. 9.4.7. Efficiency
    5. 9.5. IEnumerable<T>, IEnumerator<T>, IEnumerable, and IEnumerator
      1. 9.5.1. Types That Produce Collections
    6. 9.6. Iterators
      1. 9.6.1. Forward, Reverse, and Bidirectional Iterators
    7. 9.7. Collection Initializers
    8. 9.8. Summary
  15. 10. Delegates, Anonymous Functions, and Events
    1. 10.1. Overview of Delegates
    2. 10.2. Delegate Creation and Use
      1. 10.2.1. Single Delegate
      2. 10.2.2. Delegate Chaining
      3. 10.2.3. Iterating Through Delegate Chains
      4. 10.2.4. Unbound (Open Instance) Delegates
    3. 10.3. Events
    4. 10.4. Anonymous Methods
      1. 10.4.1. Captured Variables and Closures
      2. 10.4.2. Beware the Captured Variable Surprise
      3. 10.4.3. Anonymous Methods as Delegate Parameter Binders
    5. 10.5. The Strategy Pattern
    6. 10.6. Summary
  16. 11. Generics
    1. 11.1. Difference Between Generics and C++ Templates
    2. 11.2. Efficiency and Type Safety of Generics
    3. 11.3. Generic Type Definitions and Constructed Types
      1. 11.3.1. Generic Classes and Structs
      2. 11.3.2. Generic Interfaces
      3. 11.3.3. Generic Methods
      4. 11.3.4. Generic Delegates
      5. 11.3.5. Generic Type Conversion
      6. 11.3.6. Default Value Expression
      7. 11.3.7. Nullable Types
      8. 11.3.8. Constructed Types Control Accessibility
      9. 11.3.9. Generics and Inheritance
    4. 11.4. Constraints
      1. 11.4.1. Constraints on Nonclass Types
    5. 11.5. Co- and Contravariance
      1. 11.5.1. Covariance
      2. 11.5.2. Contravariance
      3. 11.5.3. Invariance
      4. 11.5.4. Variance and Delegates
    6. 11.6. Generic System Collections
    7. 11.7. Generic System Interfaces
    8. 11.8. Select Problems and Solutions
      1. 11.8.1. Conversion and Operators within Generic Types
      2. 11.8.2. Creating Constructed Types Dynamically
    9. 11.9. Summary
  17. 12. Threading in C#
    1. 12.1. Threading in C# and .NET
      1. 12.1.1. Starting Threads
        1. 12.1.1.1. Passing Data to New Threads
        2. 12.1.1.2. Using ParameterizedThreadStart
      2. 12.1.2. The IOU Pattern and Asynchronous Method Calls
      3. 12.1.3. States of a Thread
      4. 12.1.4. Terminating Threads
      5. 12.1.5. Halting Threads and Waking Sleeping Threads
      6. 12.1.6. Waiting for a Thread to Exit
      7. 12.1.7. Foreground and Background Threads
      8. 12.1.8. Thread-Local Storage
      9. 12.1.9. How Unmanaged Threads and COM Apartments Fit In
    2. 12.2. Synchronizing Work Between Threads
      1. 12.2.1. Lightweight Synchronization with the Interlocked Class
      2. 12.2.2. SpinLock Class
      3. 12.2.3. Monitor Class
        1. 12.2.3.1. Beware of Boxing
        2. 12.2.3.2. Pulse and Wait
      4. 12.2.4. Locking Objects
        1. 12.2.4.1. ReaderWriterLock
        2. 12.2.4.2. ReaderWriterLockSlim
        3. 12.2.4.3. Mutex
      5. 12.2.5. Semaphore
      6. 12.2.6. Events
      7. 12.2.7. Win32 Synchronization Objects and WaitHandle
    3. 12.3. Using ThreadPool
      1. 12.3.1. Asynchronous Method Calls
      2. 12.3.2. Timers
    4. 12.4. Concurrent Programming
      1. 12.4.1. Task Class
      2. 12.4.2. Parallel Class
      3. 12.4.3. Easy Entry to the Thread Pool
    5. 12.5. Thread-Safe Collection Classes
    6. 12.6. Summary
  18. 13. In Search of C# Canonical Forms
    1. 13.1. Reference Type Canonical Forms
      1. 13.1.1. Default to sealed Classes
      2. 13.1.2. Use the Non-Virtual Interface (NVI) Pattern
      3. 13.1.3. Is the Object Cloneable?
      4. 13.1.4. Is the Object Disposable?
      5. 13.1.5. Does the Object Need a Finalizer?
      6. 13.1.6. What Does Equality Mean for This Object?
        1. 13.1.6.1. Reference Types and Identity Equality
        2. 13.1.6.2. Value Equality
        3. 13.1.6.3. Overriding Object.Equals for Reference Types
      7. 13.1.7. If You Override Equals, Override GetHashCode Too
      8. 13.1.8. Does the Object Support Ordering?
      9. 13.1.9. Is the Object Formattable?
      10. 13.1.10. Is the Object Convertible?
      11. 13.1.11. Prefer Type Safety at All Times
      12. 13.1.12. Using Immutable Reference Types
    2. 13.2. Value Type Canonical Forms
      1. 13.2.1. Override Equals for Better Performance
      2. 13.2.2. Do Values of This Type Support Any Interfaces?
      3. 13.2.3. Implement Type-Safe Forms of Interface Members and Derived Methods
    3. 13.3. Summary
      1. 13.3.1. Checklist for Reference Types
      2. 13.3.2. Checklist for Value Types
  19. 14. Extension Methods
    1. 14.1. Introduction to Extension Methods
      1. 14.1.1. How Does the Compiler Find Extension Methods?
      2. 14.1.2. Under the Covers
      3. 14.1.3. Code Readability versus Code Understandability
    2. 14.2. Recommendations for Use
      1. 14.2.1. Consider Extension Methods Over Inheritance
      2. 14.2.2. Isolate Extension Methods in Separate Namespace
      3. 14.2.3. Changing a Type's Contract Can Break Extension Methods
    3. 14.3. Transforms
    4. 14.4. Operation Chaining
    5. 14.5. Custom Iterators
      1. 14.5.1. Borrowing from Functional Programming
    6. 14.6. The Visitor Pattern
    7. 14.7. Summary
  20. 15. Lambda Expressions
    1. 15.1. Introduction to Lambda Expressions
      1. 15.1.1. Lambda Expressions and Closures
        1. 15.1.1.1. Closures in C# 1.0
        2. 15.1.1.2. Closures in C# 2.0
      2. 15.1.2. Lambda Statements
    2. 15.2. Expression Trees
      1. 15.2.1. Operating on Expressions
      2. 15.2.2. Functions as Data
    3. 15.3. Useful Applications of Lambda Expressions
      1. 15.3.1. Iterators and Generators Revisited
      2. 15.3.2. More on Closures (Variable Capture) and Memoization
      3. 15.3.3. Currying
      4. 15.3.4. Anonymous Recursion
    4. 15.4. Summary
  21. 16. LINQ: Language Integrated Query
    1. 16.1. A Bridge to Data
      1. 16.1.1. Query Expressions
      2. 16.1.2. Extension Methods and Lambda Expressions Revisited
    2. 16.2. Standard Query Operators
    3. 16.3. C# Query Keywords
      1. 16.3.1. The from Clause and Range Variables
      2. 16.3.2. The join Clause
      3. 16.3.3. The where Clause and Filters
      4. 16.3.4. The orderby Clause
      5. 16.3.5. The select Clause and Projection
      6. 16.3.6. The let Clause
      7. 16.3.7. The group Clause
      8. 16.3.8. The into Clause and Continuations
    4. 16.4. The Virtues of Being Lazy
      1. 16.4.1. C# Iterators Foster Laziness
      2. 16.4.2. Subverting Laziness
      3. 16.4.3. Executing Queries Immediately
      4. 16.4.4. Expression Trees Revisited
    5. 16.5. Techniques from Functional Programming
      1. 16.5.1. Custom Standard Query Operators and Lazy Evaluation
      2. 16.5.2. Replacing foreach Statements
    6. 16.6. Summary
  22. 17. Dynamic Types
    1. 17.1. What does dynamic Mean?
    2. 17.2. How Does dynamic Work?
      1. 17.2.1. The Great Unification
      2. 17.2.2. Call Sites
      3. 17.2.3. Objects with Custom Dynamic Behavior
      4. 17.2.4. Efficiency
      5. 17.2.5. Boxing with Dynamic
    3. 17.3. Dynamic Conversions
      1. 17.3.1. Implicit Dynamic Expressions Conversion
    4. 17.4. Dynamic Overload Resolution
    5. 17.5. Dynamic Inheritance
      1. 17.5.1. You Cannot Derive from dynamic
      2. 17.5.2. You Cannot Implement dynamic Interfaces
      3. 17.5.3. You Can Derive From Dynamic Base Types
    6. 17.6. Duck Typing in C#
    7. 17.7. Limitations of dynamic Types
    8. 17.8. ExpandoObject: Creating Objects Dynamically
    9. 17.9. Summary