More Effective C#: 50 Specific Ways to Improve Your C#

More Effective C#: 50 Specific Ways to Improve Your C#

by Bill Wagner
More Effective C#: 50 Specific Ways to Improve Your C#

More Effective C#: 50 Specific Ways to Improve Your C#

by Bill Wagner

Paperback(2nd ed.)

$44.99 
  • SHIP THIS ITEM
    Qualifies for Free Shipping
  • PICK UP IN STORE
    Check Availability at Nearby Stores

Related collections and offers


Overview

In More Effective C#, Second Edition, world-renowned .NET expert Bill Wagner identifies and illuminates 50 intermediate-to-advanced techniques for writing exceptionally robust and well-performing C# 7.0 code. Reflecting the growing sophistication of the C# language and its development community, Wagner presents powerful new solutions to problems you’re likely to encounter every day.

Through three editions of Effective C#, Wagner’s clear explanations, expert tips, and realistic code examples have proven invaluable to hundreds of thousands of developers. With the publication of this title, Effective C#, Third Edition, and More Effective C#, Second Edition, have been completely reorganized to provide clear explanations, expert tips, and realistic code examples in a cohesive package for modern C#. More Effective C#, Second Edition, brings the same proven approach to the new features in C# 7.0, helping you perform familiar tasks more efficiently and effectively.

Drawing on his unsurpassed C# experience and key role on global C# standards committees, Wagner addresses object-oriented, functional, and service-oriented approaches to managing data with C#; better ways to express your intent to users and other programmers; and new opportunities to leverage powerful asynchronous and dynamic programming techniques.

  • Use properties instead of accessible data members (Item 1)
  • Distinguish between value and reference types (Item 4)
  • Understand relationships among multiple concepts of equality (Item 9)
  • Avoid conversion operators in your APIs (Item 11)
  • Understand how interface and virtual methods differ (Item 15)
  • Avoid overloading methods defined in base classes (Item 19)
  • Create method groups that are clear, minimal, and complete (Item 22)
  • Enable immediate error reporting in iterators and async methods (Item 26)
  • Use async methods for async work (Item 27)
  • Avoid thread allocations and context switches (Item 30)
  • Construct PLINQ parallel algorithms with exceptions in mind (Item 36)
  • Use the thread pool instead of creating threads (Item 37)
  • Use BackgroundWorker for cross-thread communication (Item 38)
  • Use the smallest possible scope for lock handles (Item 41)
  • Understand the pros and cons of dynamic programming (Item 43)
  • Make full use of the expression API (Item 46)
  • Minimize dynamic objects in public APIs (Item 47)

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

Content Update Program: This title is no longer part of the Content Update Program. The content is final, and no further updates will be released.


Product Details

ISBN-13: 9780672337888
Publisher: Pearson Education
Publication date: 08/15/2017
Series: Effective Software Development Series
Edition description: 2nd ed.
Pages: 304
Product dimensions: 6.90(w) x 9.00(h) x 0.70(d)

About the Author

Bill Wagner is one of the world’s foremost C# developers, a member of the ECMA C# Standards Committee, and author of all three editions of Effective C#. He is president of the Humanitarian Toolbox, has been awarded Microsoft Regional Director and .NET MVP for 11 years, and was recently appointed to the .NET Foundation Advisory Council. Wagner has helped companies ranging from start-ups to enterprises to improve development processes and grow development teams. Now a member of Microsoft’s .NET Core content team, he creates developer learning materials on the C# language and .NET Core.

Table of Contents

Introduction xi

Chapter 1: Working with Data Types 1

Item 1: Use Properties Instead of Accessible Data Members 1

Item 2: Prefer Implicit Properties for Mutable Data 8

Item 3: Prefer Immutability for Value Types 12

Item 4: Distinguish Between Value Types and Reference Types 18

Item 5: Ensure That 0 Is a Valid State for Value Types 24

Item 6: Ensure That Properties Behave Like Data 28

Item 7: Limit Type Scope by Using Tuples 34

Item 8: Define Local Functions on Anonymous Types 39

Item 9: Understand the Relationships Among the Many Different Concepts of Equality 45

Item 10: Understand the Pitfalls of GetHashCode() 54

Chapter 2: API Design 61

Item 11: Avoid Conversion Operators in Your APIs 61

Item 12: Use Optional Parameters to Minimize Method Overloads 65

Item 13: Limit Visibility of Your Types 69

Item 14: Prefer Defining and Implementing Interfaces to Inheritance 73

Item 15: Understand How Interface Methods Differ from Virtual Methods 82

Item 16: Implement the Event Pattern for Notifications 86

Item 17: Avoid Returning References to Internal Class Objects 93

Item 18: Prefer Overrides to Event Handlers 97

Item 19: Avoid Overloading Methods Defined in Base Classes 100

Item 20: Understand How Events Increase Runtime Coupling Among Objects 104

Item 21: Declare Only Nonvirtual Events 107

Item 22: Create Method Groups That Are Clear, Minimal, and Complete 113

Item 23: Give Partial Classes Partial Methods for Constructors, Mutators, and Event Handlers 120

Item 24: Avoid ICloneable Because It Limits Your Design Choices 125

Item 25: Limit Array Parameters to params Arrays 129

Item 26: Enable Immediate Error Reporting in Iterators and Async Methods Using Local Functions 134

Chapter 3: Task-Based Asynchronous Programming 139

Item 27: Use Async Methods for Async Work 139

Item 28: Never Write async void Methods 143

Item 29: Avoid Composing Synchronous and Asynchronous Methods 149

Item 30: Use Async Methods to Avoid Thread Allocations and Context Switches 154

Item 31: Avoid Marshalling Context Unnecessarily 156

Item 32: Compose Asynchronous Work Using Task Objects 160

Item 33: Consider Implementing the Task Cancellation Protocol 166

Item 34: Cache Generalized Async Return Types 173

Chapter 4: Parallel Processing 177

Item 35: Learn How PLINQ Implements Parallel Algorithms 177

Item 36: Construct Parallel Algorithms with Exceptions in Mind 189

Item 37: Use the Thread Pool Instead of Creating Threads 195

Item 38: Use BackgroundWorker for Cross-Thread Communication 201

Item 39: Understand Cross-Thread Calls in XAML Environments 205

Item 40: Use lock() as Your First Choice for Synchronization 214

Item 41: Use the Smallest Possible Scope for Lock Handles 221

Item 42: Avoid Calling Unknown Code in Locked Sections 225

Chapter 5: Dynamic Programming 229

Item 43: Understand the Pros and Cons of Dynamic Typing 229

Item 44: Use Dynamic Typing to Leverage the Runtime Type of Generic Type Parameters 238

Item 45: Use DynamicObject or IDynamicMetaObjectProvider for Data-Driven Dynamic Types 242

Item 46: Understand How to Use the Expression API 253

Item 47: Minimize Dynamic Objects in Public APIs 259

Chapter 6: Participate in the Global C# Community 267

Item 48: Seek the Best Answer, Not the Most Popular Answer 267

Item 49: Participate in Specs and Code 269

Item 50: Consider Automating Practices with Analyzers 271

Index 273

Preface

Introduction

When Anders Hejlsberg first showed Language-Integrated Query (LINQ) to the world at the 2005 Professional Developers Conference (PDC), the C# programming world changed. LINQ justified several new features in the C# language: extension methods, local variable type inference, lambda expressions, anonymous types, object initializers, and collection initializers. C# 2.0 set the stage for LINQ by adding generics, iterators, static classes, nullable types, property accessor accessibility, and anonymous delegates. But all these features are useful outside LINQ: They are handy for many programming tasks that have nothing to do with querying data sources.

This book provides practical advice about the features added to the C# programming language in the 2.0 and 3.0 releases, along with advanced features that were not covered in my earlier Effective C#: 50 Specific Ways to Improve Your C# (Addison-Wesley, 2004). The items in More Effective C# reflect the advice I give developers who are adopting C# 3.0 in their professional work. There’s a heavy emphasis on generics, an enabling technology for everything in C# 2.0 and 3.0. I discuss the new features in C# 3.0; rather than organize the topics by language feature, I present these tips from the perspective of recommendations about the programming problems that developers can best solve by using these new features.

Consistent with the other books in the Effective Software Development Series, this book contains self-contained items detailing specific advice about how to use C#. The items are organized to guide you from using C# 1.x to using C# 3.0 in the best way.

Generics arean enabling technology for all new idioms that are part of C# 3.0. Although only the first chapter specifically addresses generics, you’ll find that they are an integral part of almost every item. After reading this book, you’ll be much more comfortable with generics and metaprogramming.

Of course, much of the book discusses how to use C# 3.0 and the LINQ query syntax in your code. The features added in C# 3.0 are very useful in their own right, whether or not you are querying data sources. These changes in the language are so extensive, and LINQ is such a large part of the justification for those changes, that each warrants its own chapter. LINQ and C# 3.0 will have a profound impact on how you write code in C#. This book will make that transition easier.

Who Should Read This Book?

This book was written for professional software developers who use C#. It assumes that you have some familiarity with C# 2.0 and C# 3.0. Scott Meyers counseled me that an Effective book should be a developer’s second book on a subject. This book does not include tutorial information on the new language features added as the language has evolved. Instead, I explain how you can integrate these features into your ongoing development activities. You’ll learn when to leverage the new language features in your development activities, and when to avoid certain practices that will lead to brittle code.

In addition to some familiarity with the newer features of the C# language, you should have an understanding of the major components that make up the .NET Framework: the .NET CLR (Common Language Runtime), the .NET BCL (Base Class Library), and the JIT (Just In Time) compiler. This book doesn’t cover .NET 3.0 components, such as WCF (Windows Communication Foundation), WPF (Windows Presentation Foundation), and WF (Windows Workflow Foundation). However, all the idioms presented apply to those components as well as any other .NET Framework components you happen to prefer.

About the Content

Generics are the enabling technology for everything else added to the C# language since C# 1.1. Chapter 1 covers generics as a replacement for System.Object and casts and then moves on to discuss advanced techniques such as constraints, generic specialization, method constraints, and backward compatibility. You’ll learn several techniques in which generics will make it easier to express your design intent.

Multicore processors are already ubiquitous, with more cores being added seemingly every day. This means that every C# developer needs to have a solid understanding of the support provided by the C# language for multithreaded programming. Although one chapter can’t cover everything you need to be an expert, Chapter 2 discusses the techniques you’ll need every day when you write multithreaded applications.

Chapter 3 explains how to express modern design idioms in C#. You’ll learn the best way to express your intent using the rich palette of C# language features. You’ll see how to leverage lazy evaluation, create composable interfaces, and avoid confusion among the various language elements in your public interfaces.

Chapter 4 discusses how to use the enhancements in C# 3.0 to solve the programming challenges you face every day. You’ll see when to use extension methods to separate contracts from implementation, how to use C# closures effectively, and how to program with anonymous types.

Chapter 5 explains LINQ and query syntax. You’ll learn how the compiler maps query keywords to method calls, how to distinguish between delegates and expression trees (and convert between them when needed), and how to escape queries when you’re looking for scalar results.

Chapter 6 covers those items that defy classification. You’ll learn how to define partial classes, work with nullable types, and avoid covariance and contravariance problems with array parameters.

Regarding the Sample Code

The samples in this book are not complete programs. They are the smallest snippets of code possible that illustrate the point. In several samples the method names substitute for a concept, such as AllocateExpensiveResource(). Rather than read pages of code, you can grasp the concept and quickly apply it to your professional development. Where methods are elided, the name implies what’s important about the missing method.

In all cases, you can assume that the following namespaces are specified:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

Where types are used from other namespaces, I’ve explicitly included the namespace in the type.

In the first three chapters, I often show C# 2.0 and C# 3.0 syntax where newer syntax is preferred but not required. In Chapters 4 and 5 I assume that you would use the 3.0 syntax.

From the B&N Reads Blog

Customer Reviews