O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A Programmer's Guide to C# 5.0, 4th 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. Cover
    2. Title
    3. Copyright
    4. Dedication
    5. Contents at a Glance
    6. Contents
    7. Preface
    8. About the Authors
    9. About the Technical Reviewer
    10. Acknowledgments
    11. Introduction
    12. Chapter 1: C# and the .NET Runtime and Libraries
    13. 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#
        1. Tools of Note
    14. Chapter 3: Classes 101
      1. A Simple Class
      2. Member Functions
      3. ref and out Parameters
      4. Overloading
    15. 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
    16. 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
        1. Caller Beware
        2. Caller Confuse
        3. Caller Inform
      5. User-Defined Exception Classes
      6. Finally
      7. Top-Level Exception Handlers
      8. Efficiency and Overhead
      9. Design Guidelines
        1. Exceptions Are Exceptional
        2. Choosing the Right Exception for Wrapping
        3. Exceptions Should be as Specific as Possible
        4. Retry Logic
        5. Rethrowing
        6. Catch Only if You Have Something Useful to Do
    17. Chapter 6: Member Accessibility and Overloading
      1. Class Accessibility
      2. Using Internal on Members
        1. Expanding Internal Accessibility
      3. Protected
        1. Internal Protected
      4. The Interaction of Class and Member Accessibility
      5. Accessability Summary
      6. Method Overloading
        1. Method Hiding
        2. Better Conversions
      7. Variable-Length Parameter Lists
      8. Default Arguments
      9. Named Arguments
    18. Chapter 7: Other Class Details
      1. Nested Classes
        1. Other Nesting
      2. Anonymous Types
      3. Creation, Initialization, Destruction
        1. Constructors
        2. Initialization
        3. Destructors
        4. Managing Nonmemory Resources
        5. IDisposable and the Using Statement
        6. IDisposable and Longer-Lived Objects
      4. Static Fields
      5. Static Member Functions
      6. Static Constructors
      7. Constants
      8. Read-Only Fields
      9. Extension Methods
        1. Usage Guidelines
      10. Object Initializers
      11. Static Classes
      12. Partial Classes and Methods
    19. 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
    20. 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
        1. Explicit Interface Implementation
        2. Implementation Hiding
      7. Interfaces Based on Interfaces
      8. Interfaces and Structs
    21. Chapter 10: Versioning and Aliases
      1. A Versioning Example
      2. Coding for Versioning
      3. Type Aliases
      4. External Assembly Aliases
    22. Chapter 11: Statements and Flow of Execution
      1. Selection Statements
        1. If
        2. Switch
      2. Iteration Statements
        1. While
        2. Do
        3. For
        4. Foreach
      3. Jump Statements
        1. Break
        2. Continue
        3. Goto
        4. Return
      4. Other Statements
        1. lock
        2. using
        3. try, catch, and finally
        4. checked and unchecked
        5. yield
    23. Chapter 12: Variable Scoping and Definite Assignment
      1. Definite Assignment
        1. Definite Assignment and Class Members
        2. Definite Assignment and Arrays
    24. Chapter 13: Operators and Expressions
      1. Operator Precedence
      2. Built-in Operators
      3. User-Defined Operators
      4. Numeric Promotions
      5. Arithmetic Operators
        1. Unary Plus (+)
        2. Unary Minus ()
        3. Bitwise Complement (~)
        4. Addition (+)
        5. Subtraction ()
        6. Multiplication (*)
        7. Division (/)
        8. Remainder (%)
        9. Shift (<< and >>)
        10. Increment and Decrement (++ and --)
      6. Relational and Logical Operators
        1. Logical Negation (!)
        2. Relational Operators over
        3. Logical Operators
        4. Conditional Operator (?:)
        5. Null Coalescing Operator (??)
      7. Assignment Operators
        1. Simple Assignment
        2. Compound Assignment
      8. Type Operators
        1. typeof
        2. is
        3. as
      9. Checked and Unchecked Expressions
      10. Type Inference (var)
        1. Best Practices
    25. Chapter 14: Conversions
      1. Numeric Types
        1. Conversions and Member Lookup
        2. Explicit Numeric Conversions
        3. Checked Conversions
      2. Conversions of Classes (Reference Types)
        1. From an Object to the Base Class of an Object
        2. From an Object to an Interface the Object Implements
        3. From an Object to an Interface the Object Might Implement
        4. From One Interface Type to Another
      3. Conversions of Structs (Value Types)
    26. Chapter 15: Arrays
      1. Array Initialization
      2. Multidimensional and Jagged Arrays
        1. Multidimensional Arrays
        2. Jagged Arrays
      3. Arrays of Reference Types
      4. Array Conversions
      5. The System.Array Type
        1. Sorting and Searching
        2. Reverse
    27. 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
    28. Chapter 17: Generic Types
      1. A List of Integers
      2. Constraints
        1. Interface Constraints
        2. Base Class Constraints
        3. Class and Struct Constraints
        4. Multiple Constraints
      3. The Default Value of a Type
      4. Generic Interfaces and Inheritance
      5. Generic Methods
      6. Generic Delegates
      7. Covariance and Contravariance
        1. Contravariance
      8. Generics and Efficiency
      9. Generic Naming Guidelines
    29. Chapter 18: Indexers, Enumerators, and Iterators
      1. Indexing with an Integer Index
      2. Indexing with a String Index
      3. Indexing with Multiple Parameters
        1. Design Guidelines for Indexers
      4. Object Enumeration
        1. Enumerators and Foreach
        2. Enabling Enumeration
        3. Iterators
        4. Named Iterators
        5. Iterators and Generic Types
        6. Iterators and Resource Management
    30. 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
        1. Regular Expression Options
        2. More Complex Parsing
    31. Chapter 20: Enumerations
      1. A Line-Style Enumeration
      2. Enumeration Base Types
      3. Initialization
      4. Bit Flag Enums
      5. Conversions
      6. The System.Enum Type
    32. Chapter 21: Attributes
      1. Using Attributes
        1. A Few More Details
      2. An Attribute of Your Own
        1. Attribute Usage
        2. Attribute Parameters
      3. Fetching Attribute Values
    33. 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
        1. Implementation
      7. Variable Capturing
      8. Guidelines
        1. Parameter Naming
        2. Method, Anonymous Delegate, or Lambda?
    34. 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
    35. Chapter 24: Dynamic Typing
      1. The dynamic Keyword
      2. Dynamic Failure
      3. Generic Arithmetic with dynamic
      4. Usage Guidelines
    36. 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
        1. Implicit Conversions Are Safe Conversions
        2. Define the Conversion in the More Complex Type
        3. One Conversion to and from a Hierarchy
        4. Add Conversions Only As Needed
        5. Conversions That Operate in Other Languages
      6. How It Works
        1. Conversion Lookup
    37. Chapter 26: Operator Overloading
      1. Unary Operators
      2. Binary Operators
      3. An Example
      4. Restrictions
      5. Guidelines
      6. A Complex Number Class
    38. Chapter 27: Nullable Types
      1. A Simple Example
      2. Using Nullable Types
      3. Null Coalescing
      4. Nullable Types and Equality
    39. Chapter 28: Linq to Objects
      1. Getting Started with Linq to Objects
        1. Filtering Data
        2. Transforming Data
        3. Stringing Sequences Together
        4. Behind the Curtain
        5. Query Expressions
      2. A Sequence Method of Your Own
      3. Sequence Method Reference
        1. Aggregate Methods
        2. Transformational Methods
        3. Extraction Methods
        4. Subset Methods
        5. Ordering Methods
        6. Whole Sequence Methods
        7. Conditional Methods
        8. Generator Methods
        9. Join()
        10. GroupBy()
    40. Chapter 29: Linq to XML
      1. Rockin’ It “Old School”
      2. Linq to XML
        1. Creating XML
        2. Namespaces
        3. Parsing XML
    41. 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
    42. Chapter 31: Other Language Details
      1. The Main Function
        1. Returning an Int Status
        2. Command-Line Parameters
        3. Multiple Mains
      2. Preprocessing
        1. Preprocessing Directives
        2. Other Preprocessor Functions
      3. Lexical Details
        1. Identifiers
        2. Keywords
      4. Literals
        1. Boolean
        2. Integer
        3. Real
        4. Character
        5. String
        6. Comments
      5. Expanding Internal Accessibility
    43. Chapter 32: Making Friends with the .NET Framework
      1. ToString( )
      2. Object Equality
      3. Hashes and GetHashCode( )
      4. Design Guidelines
        1. Value Type Guidelines
        2. Reference Type Guidelines
    44. Chapter 33: System.Array and the Collection Classes
      1. Sorting and Searching
        1. Specifying a Sort Order
        2. Multiple Sort Orders
        3. Ad Hoc Sorting Orders
        4. Overloading Relational Operators
        5. Advanced Use of Hash Codes
      2. Synchronized Collections
      3. Case-Insensitive Collections
      4. Collection Classes
      5. Design Guidelines
    45. Chapter 34: Threading
      1. Data Protection and Synchronization
        1. A Slightly Broken Example
        2. Protection Techniques
        3. Mutexes
      2. Access Reordering and Volatile
        1. Singletons
      3. Threads
        1. Joining
        2. Waiting with WaitHandle
    46. Chapter 35: Asynchronous and Parallel Programming
      1. History
      2. Thread Pools
      3. Introducing Tasks
        1. Tasks with Return Values
      4. The async and await Keywords
      5. Tasks and Completion Ports
      6. Tasks and Parallel Operations
      7. Data Parallelism
      8. PLinq
      9. Design Guidelines
    47. Chapter 36: Execution-Time Code Generation
      1. Loading Assemblies
        1. Making It Dynamic
      2. Code Generation at Runtime
        1. Expressing the Algorithm in Code
        2. Translating and Loading the Code
        3. Calling the Code
        4. Design Guidelines
      3. The C# Compiler As a Service
    48. Chapter 37: Interop
      1. COM Objects
      2. Calling Native DLL Functions
        1. Pointers and Declarative Pinning
        2. Structure Layout
        3. Calling a Function with a Structure Parameter
        4. Fixed-Size Buffers
        5. Hooking Up to a Windows Callback
        6. The Marshal Class
        7. Design Guidelines
    49. Chapter 38: .NET Base Class Library Overview
      1. Numeric Formatting
        1. Standard Format Strings
        2. Custom Format Strings
      2. Date and Time Formatting
      3. Custom Object Formatting
      4. Numeric Parsing
        1. Using TryParse to Avoid Exceptions
      5. Input and Output
        1. Binary
        2. Text
        3. XML
        4. Writing Files
        5. Reading Files
        6. Traversing Directories
        7. Starting Processes
      6. Serialization
      7. Custom Serialization
      8. XML Serialization
        1. XmlSerializer or XElement?
      9. Reading Web Pages
      10. Accessing Environment Settings
    50. Chapter 39: Deeper into C#
      1. C# Style
        1. Naming
      2. Guidelines for the Library Author
        1. Class Naming
      3. Unsafe Context
      4. XML Documentation
        1. Compiler Support Tags
        2. Generating Real Documentation
        3. XML Documentation Tags
        4. XML Include Files
      5. Garbage Collection in the .NET Runtime
        1. Allocation
        2. Mark and Compact
        3. Generations
        4. Finalization
        5. Controlling GC Behavior
      6. Deeper Reflection
        1. Listing All the Types in an Assembly
        2. Finding Members
        3. Invoking Functions
        4. Dealing with Generics
      7. Optimizations
    51. 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
        1. BooleanSwitch
        2. TraceSwitch
        3. User-Defined Switch
        4. Capturing Process Metadata
      6. Improving Your Debugger Experience
        1. The DebuggerDisplay Attribute
        2. Changing the Display of Existing Types
        3. Changing the Detail Display for a Type
        4. Full Customization of the Detail Display
        5. Debugger Visualizers
      7. Debugging into the .NET Framework
    52. Chapter 41: IDEs and Utilities
      1. IDEs
      2. Unit Testing and TDD
        1. Test Utilities/Runners
      3. Disassemblers and Decompilers
      4. Obfuscators
      5. General Productivity
      6. Regular Expressions
      7. Profilers
      8. Answering Your Questions
    53. Index