You are previewing A Programmer's Guide to C# 5.0, Fourth Edition.
O'Reilly logo
A Programmer's Guide to C# 5.0, Fourth Edition

Book Description

A Programmer's Guide to C# 5.0 is a book for software developers who want to truly understand C#. Whether you've worked with C# before or with another general-purpose programming language, each fast-paced, focused chapter will take you straight to the heart of a feature of C# and show you why it works the way it does.

Written by one-time C# Test Lead, Program Manager, and member of the original C# language design team, this book is an ideal companion to the C# Language Specification, and works both as a tutorial and as a reference guide. Now in its fourth edition, you will find up-to-date coverage of all the latest C# features, including Linq, covariance and contravariance, and async support.

You'll learn how to:

  • Use C# features effectively, in the way they were intended

  • Apply the newest C# features to your coding problems

  • Streamline your database code using LINQ

  • Use async support and the task parallel library to improve performance.

  • Program more efficiently, effectively, and with real insight into this mature and exciting language, with A Programmer's Guide to C# 5.0.

  • What you'll learn

  • Learn the "why" behind C# features.

  • Learn how to use C# features effectively, in the way they were designed.

  • Apply the newest C# features to your coding problems.

  • Streamline your database code using Linq.

  • Use Async support to simplify your code.

  • Who this book is for

    A Programmer's Guide to C# is aimed at software developers who want to improve their proficiency at using the C# language. No prior knowledge of .NET is required, but the book does assume experience with a general-purpose programming language.

    Table of Contents

    1. Title
    2. Dedication
    3. Contents at a Glance
    4. Contents
    5. Preface
    6. About the Authors
    7. About the Technical Reviewer
    8. Acknowledgments
    9. Introduction
    10. Chapter 1: C# and the .NET Runtime and Libraries
    11. Chapter 2: C# QuickStart and Developing in C#
      1. Hello, Universe
      2. Namespace and Using Statements
      3. Namespaces and Assemblies
      4. Basic Data Types
      5. Classes, Structs, and Interfaces
      6. Statements
      7. Enums
      8. Delegates and Events
      9. Properties and Indexers
      10. Attributes
      11. Developing in C#
    12. Chapter 3: Classes 101
      1. A Simple Class
      2. Member Functions
      3. ref and out Parameters
      4. Overloading
    13. Chapter 4: Base Classes and Inheritance
      1. The Engineer Class
      2. Simple Inheritance
      3. Arrays of Engineers
      4. Virtual Functions
      5. Abstract Classes
      6. Sealed Classes and Methods
    14. Chapter 5: Exception Handling
      1. What’s Wrong with Return Codes?
      2. Trying and Catching
      3. Choosing the Catch Block
      4. Passing Exceptions on to the Caller
      5. User-Defined Exception Classes
      6. Finally
      7. Top-Level Exception Handlers
      8. Efficiency and Overhead
      9. Design Guidelines
    15. Chapter 6: Member Accessibility and Overloading
      1. Class Accessibility
      2. Using Internal on Members
      3. Protected
      4. The Interaction of Class and Member Accessibility
      5. Accessability Summary
      6. Method Overloading
      7. Variable-Length Parameter Lists
      8. Default Arguments
      9. Named Arguments
    16. Chapter 7: Other Class Details
      1. Nested Classes
      2. Anonymous Types
      3. Creation, Initialization, Destruction
      4. Static Fields
      5. Static Member Functions
      6. Static Constructors
      7. Constants
      8. Read-Only Fields
      9. Extension Methods
      10. Object Initializers
      11. Static Classes
      12. Partial Classes and Methods
    17. Chapter 8: Structs (Value Types)
      1. A Point Struct
      2. Boxing and Unboxing
      3. Structs and Constructors
      4. Mutable Structs
      5. Design Guidelines
      6. Immutable Classes
    18. Chapter 9: Interfaces
      1. A Simple Example
      2. Working with Interfaces
      3. The as Operator
      4. Interfaces and Inheritance
      5. Design Guidelines
      6. Multiple Implementation
      7. Interfaces Based on Interfaces
      8. Interfaces and Structs
    19. Chapter 10: Versioning and Aliases
      1. A Versioning Example
      2. Coding for Versioning
      3. Type Aliases
      4. External Assembly Aliases
    20. Chapter 11: Statements and Flow of Execution
      1. Selection Statements
      2. Iteration Statements
      3. Jump Statements
      4. Other Statements
    21. Chapter 12: Variable Scoping and Definite Assignment
      1. Definite Assignment
    22. Chapter 13: Operators and Expressions
      1. Operator Precedence
      2. Built-in Operators
      3. User-Defined Operators
      4. Numeric Promotions
      5. Arithmetic Operators
      6. Relational and Logical Operators
      7. Assignment Operators
      8. Type Operators
      9. Checked and Unchecked Expressions
      10. Type Inference (var)
    23. Chapter 14: Conversions
      1. Numeric Types
      2. Conversions of Classes (Reference Types)
      3. Conversions of Structs (Value Types)
    24. Chapter 15: Arrays
      1. Array Initialization
      2. Multidimensional and Jagged Arrays
      3. Arrays of Reference Types
      4. Array Conversions
      5. The SystemArray Type
    25. Chapter 16: Properties
      1. Accessors
      2. Properties and Inheritance
      3. Using Properties
      4. Side Effects When Setting Values
      5. Static Properties
      6. Property Efficiency
      7. Property Accessibility
      8. Virtual Properties
      9. Automatic Properties
      10. Properties vs Fields
    26. Chapter 17: Generic Types
      1. A List of Integers
      2. Constraints
      3. The Default Value of a Type
      4. Generic Interfaces and Inheritance
      5. Generic Methods
      6. Generic Delegates
      7. Covariance and Contravariance
      8. Generics and Efficiency
      9. Generic Naming Guidelines
    27. Chapter 18: Indexers, Enumerators, and Iterators
      1. Indexing with an Integer Index
      2. Indexing with a String Index
      3. Indexing with Multiple Parameters
      4. Object Enumeration
    28. Chapter 19: Strings
      1. Operations
      2. String Literals
      3. String Encodings and Conversions
      4. Converting Objects to Strings
      5. An Example
      6. StringBuilder
      7. Regular Expressions
    29. Chapter 20: Enumerations
      1. A Line-Style Enumeration
      2. Enumeration Base Types
      3. Initialization
      4. Bit Flag Enums
      5. Conversions
      6. The SystemEnum Type
    30. Chapter 21: Attributes
      1. Using Attributes
      2. An Attribute of Your Own
      3. Fetching Attribute Values
    31. Chapter 22: Delegates, Anonymous Methods, and Lambdas
      1. Using Delegates
      2. Delegates to Instance Members
      3. Multicasting
      4. Delegates As Static Members
      5. Anonymous Methods
      6. Lambdas
      7. Variable Capturing
      8. Guidelines
    32. Chapter 23: Events
      1. A Simple Example Using Delegates
      2. Add and Remove Functions
      3. Safe Event Invocation
      4. EventHandler <T>
      5. Custom Add and Remove
    33. Chapter 24: Dynamic Typing
      1. The dynamic Keyword
      2. Dynamic Failure
      3. Generic Arithmetic with dynamic
      4. Usage Guidelines
    34. Chapter 25: User-Defined Conversions
      1. A Simple Example
      2. Pre- and Post-Conversions
      3. Conversions Between Structs
      4. Classes and Pre- and Post-Conversions
      5. Design Guidelines
      6. How It Works
    35. Chapter 26: Operator Overloading
      1. Unary Operators
      2. Binary Operators
      3. An Example
      4. Restrictions
      5. Guidelines
      6. A Complex Number Class
    36. Chapter 27: Nullable Types
      1. A Simple Example
      2. Using Nullable Types
      3. Null Coalescing
      4. Nullable Types and Equality
    37. Chapter 28: Linq to Objects
      1. Getting Started with Linq to Objects
      2. A Sequence Method of Your Own
      3. Sequence Method Reference
    38. Chapter 29: Linq to XML
      1. Rockin’ It “Old School”
      2. Linq to XML
    39. Chapter 30: Linq to SQL
      1. Connecting by Hand
      2. Query and Class Generation
      3. Linq to SQL
      4. Joining Tables
      5. How It Works
      6. Modifying Data
      7. Stored Procedures
      8. Linq to Entities
    40. Chapter 31: Other Language Details
      1. The Main Function
      2. Preprocessing
      3. Lexical Details
      4. Literals
      5. Expanding Internal Accessibility
    41. Chapter 32: Making Friends with the .NET Framework
      1. ToString( )
      2. Object Equality
      3. Hashes and GetHashCode( )
      4. Design Guidelines
    42. Chapter 33: System.Array and the Collection Classes
      1. Sorting and Searching
      2. Synchronized Collections
      3. Case-Insensitive Collections
      4. Collection Classes
      5. Design Guidelines
    43. Chapter 34: Threading
      1. Data Protection and Synchronization
      2. Access Reordering and Volatile
      3. Threads
    44. Chapter 35: Asynchronous and Parallel Programming
      1. History
      2. Thread Pools
      3. Introducing Tasks
      4. The async and await Keywords
      5. Tasks and Completion Ports
      6. Tasks and Parallel Operations
      7. Data Parallelism
      8. PLinq
      9. Design Guidelines
    45. Chapter 36: Execution-Time Code Generation
      1. Loading Assemblies
      2. Code Generation at Runtime
      3. The C# Compiler As a Service
    46. Chapter 37: Interop
      1. COM Objects
      2. Calling Native DLL Functions
    47. Chapter 38: .NET Base Class Library Overview
      1. Numeric Formatting
      2. Date and Time Formatting
      3. Custom Object Formatting
      4. Numeric Parsing
      5. Input and Output
      6. Serialization
      7. Custom Serialization
      8. XML Serialization
      9. Reading Web Pages
      10. Accessing Environment Settings
    48. Chapter 39: Deeper into C#
      1. C# Style
      2. Guidelines for the Library Author
      3. Unsafe Context
      4. XML Documentation
      5. Garbage Collection in the NET Runtime
      6. Deeper Reflection
      7. Optimizations
    49. Chapter 40: Logging and Debugging Techniques
      1. Conditional Methods
      2. Asserts
      3. Changing the Assert Behavior
      4. Adding Other Messages to Debug or Trace Output
      5. Using Switches to Control Debug and Trace
      6. Improving Your Debugger Experience
      7. Debugging into the NET Framework
    50. Chapter 41: IDEs and Utilities
      1. IDEs
      2. Unit Testing and TDD
      3. Disassemblers and Decompilers
      4. Obfuscators
      5. General Productivity
      6. Regular Expressions
      7. Profilers
      8. Answering Your Questions
    51. Index