Effective C#: 50 Specific Ways to Improve Your C#, Third Edition

Book description

In Effective C#, Third Edition, respected .NET expert Bill Wagner identifies 50 ways to harness the full power of the C# 6.0 language to write exceptionally robust, efficient, and well-performing code. Reflecting the growing sophistication of the C# language and its development community, Wagner has identified dozens of new ways to write better code. This edition’s new solutions include some that take advantage of generics and several that are more focused on LINQ, as well as a full chapter of advanced best practices for working with exceptions.

Wagner’s clear, practical explanations, expert tips, and realistic code examples have made Effective C# indispensable to hundreds of thousands of developers. Drawing on his unsurpassed C# experience, he addresses everything from resource management to multicore support, and reveals how to avoid common pitfalls in the language and its .NET environment. Learn how to choose the most effective solution when multiple options exist, and how to write code that’s far easier to maintain and improve. Wagner shows how and why to

  • Prefer implicitly typed local variables (see Item 1)

  • Replace string.Format() with interpolated strings (see Item 4)

  • Express callbacks with delegates (see Item 7)

  • Make the most of .NET resource management (see Item 11)

  • Define minimal and sufficient constraints for generics (see Item 18)

  • Specialize generic algorithms using runtime type checking (see Item 19)

  • Use delegates to define method constraints on type parameters (see Item 23)

  • Augment minimal interface contracts with extension methods (see Item 27)

  • Create composable APIs for sequences (see Item 31)

  • Decouple iterations from actions, predicates, and functions (see Item 32)

  • Prefer lambda expressions to methods (see Item 38)

  • Distinguish early from deferred execution (see Item 40)

  • Avoid capturing expensive resources (see Item 41)

  • Use exceptions to report method contract failures (see Item 45)

  • Leverage side effects in exception filters (see Item 50)

  • You’re already a successful C# programmer, and this book will make you an outstanding one.

    Content Update Program: This book is part of the InformIT Content Update Program. As updates are made to C#, sections of this book will be updated or new sections will be added to match updates to the technologies. See inside for details.

    Table of contents

    1. About This E-Book
    2. Title Page
    3. Copyright Page
    4. Dedication Page
    5. Contents at a Glance
    6. Contents
    7. Introduction
      1. Who Should Read This Book?
      2. About The Content
      3. Code Conventions
      4. Providing Feedback
      5. Acknowledgments
      6. About the Author
    8. 1. C # Language Idioms
      1. Item 1: Prefer Implicitly Typed Local Variables
      2. Item 2: Prefer readonly to const
      3. Item 3: Prefer the is or as Operators to Casts
      4. Item 4: Replace string.Format() with Interpolated Strings
      5. Item 5: Prefer FormattableString for Culture-Specific Strings
      6. Item 6: Avoid String-ly Typed APIs
      7. Item 7: Express Callbacks with Delegates
      8. Item 8: Use the Null Conditional Operator for Event Invocations
      9. Item 9: Minimize Boxing and Unboxing
      10. Item 10: Use the new Modifier Only to React to Base Class Updates
    9. 2. .NET Resource Management
      1. Item 11: Understand .NET Resource Management
      2. Item 12: Prefer Member Initializers to Assignment Statements
      3. Item 13: Use Proper Initialization for Static Class Members
      4. Item 14: Minimize Duplicate Initialization Logic
      5. Item 15: Avoid Creating Unnecessary Objects
      6. Item 16: Never Call Virtual Functions in Constructors
      7. Item 17: Implement the Standard Dispose Pattern
    10. 3. Working with Generics
      1. Item 18: Always Define Constraints That Are Minimal and Sufficient
      2. Item 19: Specialize Generic Algorithms Using Runtime Type Checking
      3. Item 20: Implement Ordering Relations with IComparable<T> and IComparer<T>
      4. Item 21: Always Create Generic Classes That Support Disposable Type Parameters
      5. Item 22: Support Generic Covariance and Contravariance
      6. Item 23: Use Delegates to Define Method Constraints on Type Parameters
      7. Item 24: Do Not Create Generic Specialization on Base Classes or Interfaces
      8. Item 25: Prefer Generic Methods Unless Type Parameters Are Instance Fields
      9. Item 26: Implement Classic Interfaces in Addition to Generic Interfaces
      10. Item 27: Augment Minimal Interface Contracts with Extension Methods
      11. Item 28: Consider Enhancing Constructed Types with Extension Methods
    11. 4. Working with LINQ
      1. Item 29: Prefer Iterator Methods to Returning Collections
      2. Item 30: Prefer Query Syntax to Loops
      3. Item 31: Create Composable APIs for Sequences
      4. Item 32: Decouple Iterations from Actions, Predicates, and Functions
      5. Item 33: Generate Sequence Items as Requested
      6. Item 34: Loosen Coupling by Using Function Parameters
      7. Item 35: Never Overload Extension Methods
      8. Item 36: Understand How Query Expressions Map to Method Calls
      9. Item 37: Prefer Lazy Evaluation to Eager Evaluation in Queries
      10. Item 38: Prefer Lambda Expressions to Methods
      11. Item 39: Avoid Throwing Exceptions in Functions and Actions
      12. Item 40: Distinguish Early from Deferred Execution
      13. Item 41: Avoid Capturing Expensive Resources
      14. Item 42: Distinguish between IEnumerable and IQueryable Data Sources
      15. Item 43: Use Single() and First() to Enforce Semantic Expectations on Queries
      16. Item 44: Avoid Modifying Bound Variables
    12. 5. Exception Practices
      1. Item 45: Use Exceptions to Report Method Contract Failures
      2. Item 46: Utilize using and try/finally for Resource Cleanup
      3. Item 47: Create Complete Application-Specific Exception Classes
      4. Item 48: Prefer the Strong Exception Guarantee
      5. Item 49: Prefer Exception Filters to catch and re-throw
      6. Item 50: Leverage Side Effects in Exception Filters
    13. Index
    14. Code Snippets

    Product information

    • Title: Effective C#: 50 Specific Ways to Improve Your C#, Third Edition
    • Author(s): Bill Wagner
    • Release date: December 2016
    • Publisher(s): Addison-Wesley Professional
    • ISBN: 9780134579290