More Effective C#: 50 Specific Ways to Improve Your C# (Effective Software Development Series)

Overview

“Shining a bright light into many of the dark corners of C# 3.0, this book not only covers the ‘how,’ but also the ‘why,’ arming the reader with many field-tested methods for wringing the most from the new language features, such as LINQ, generics, and multithreading. If you are serious about developing with the C# language, you need this book.”

–Bill Craun, Principal Consultant, Ambassador Solutions, Inc.

More Effective C# is an opportunity to work beside Bill Wagner. Bill leverages his knowledge of C# and ...

See more details below
Paperback (Original)
$38.53
BN.com price
(Save 29%)$54.99 List Price

Pick Up In Store

Reserve and pick up in 60 minutes at your local store

Other sellers (Paperback)
  • All (9) from $8.92   
  • New (5) from $29.69   
  • Used (4) from $8.92   
More Effective C#: 50 Specific Ways to Improve Your C#

Available on NOOK devices and apps  
  • NOOK Devices
  • NOOK HD/HD+ Tablet
  • NOOK
  • NOOK Color
  • NOOK Tablet
  • Tablet/Phone
  • NOOK for Windows 8 Tablet
  • NOOK for iOS
  • NOOK for Android
  • NOOK Kids for iPad
  • PC/Mac
  • NOOK for Windows 8
  • NOOK for PC
  • NOOK for Mac
  • NOOK Study

Want a NOOK? Explore Now

NOOK Book (eBook)
$25.49
BN.com price
(Save 42%)$43.99 List Price

Overview

“Shining a bright light into many of the dark corners of C# 3.0, this book not only covers the ‘how,’ but also the ‘why,’ arming the reader with many field-tested methods for wringing the most from the new language features, such as LINQ, generics, and multithreading. If you are serious about developing with the C# language, you need this book.”

–Bill Craun, Principal Consultant, Ambassador Solutions, Inc.

More Effective C# is an opportunity to work beside Bill Wagner. Bill leverages his knowledge of C# and distills his expertise down to some very real advice about programming and designing applications that every serious Visual C# user should know. More Effective C# is one of those rare books that doesn’t just regurgitate syntax, but teaches you how to use the C# language.”

–Peter Ritchie, Microsoft MVP: Visual C#

More Effective C# is a great follow-up to Bill Wagner’s previous book. The extensive

C# 3.0 and LINQ coverage is extremely timely!”

–Tomas Restrepo, Microsoft MVP: Visual C++, .NET, and Biztalk Server

“As one of the current designers of C#, it is rare that I learn something new about the language by reading a book. More Effective C# is a notable exception. Gently blending concrete code and deep insights, Bill Wagner frequently makes me look at C# in a fresh light–one that really makes it shine. More Effective C# is at the surface a collection of very useful guidelines. Look again. As you read through it, you’ll find that you acquire more than just the individual pieces of advice; gradually you’ll pick up on an approach to programming in C# that is thoughtful, beautiful, and deeply pleasant. While you can make your way willy-nilly through the individual guidelines, I do recommend reading the whole book–or at least not skipping over the chapter introductions before you dive into specific nuggets of advice. There’s perspective and insight to be found there that in itself can be an important guide and inspiration for your future adventures in C#.”

–Mads Torgersen, Program Manager, Visual C#, Microsoft

“Bill Wagner has written an excellent book outlining the best practices for developers who work with the C# language. By authoring More Effective C#, he has again established himself as one of the most important voices in the C# community. Many of us already know how to use C#. What we need is advice on how to hone our skills so that we can become wiser programmers. There is no more sophisticated source of information on how to become a first-class C# developer than Bill Wagner’s book. Bill is intelligent, thoughtful, experienced, and skillful. By applying the lessons from this book to your own code, you will find many ways to polish and improve the work that you produce.”

–Charlie Calvert, Community Program Manager, Visual C#, Microsoft

In More Effective C#, Microsoft C# MVP and Regional Director Bill Wagner introduces fifty brand-new ways to write more efficient and more robust software. This all-new book follows the same format as Wagner’s best-selling Effective C# (Addison-Wesley, 2005), providing clear, practical explanations, expert tips, and plenty of realistic code examples.

Wagner shows how to make the most of powerful innovations built into Microsoft’s new C# 3.0 and .NET Framework 3.5, as well as advanced C# language capabilities not covered in his previous book. Drawing on his unsurpassed C# experience, the author reveals new best practices for working with LINQ, generics, metaprogramming, and many other features. He also uncovers practices that compromise performance or reliability and shows exactly how to avoid them.

More Effective C# shows how to

  • Use generics to express your design intent more effectively
  • Master advanced generics techniques, such as constraints, method constraints, and generic specialization
  • Use the multithreaded techniques you’ll need to work with the .NET framework every day
  • Express modern design idioms using the rich palette of C# language features
  • Successfully mix object oriented and functional programming constructs
  • Create composable interfaces and avoid confusion in public interfaces
  • Use extension methods to separate contracts from implementation
  • Program successfully with C# closures and anonymous types
  • Write more effective LINQ queries
  • Make the most of LINQ Lazy Evaluation Queries and Lambda Expressions
  • Distinguish and convert between delegates and expression trees
  • Efficiently utilize nullable types and partial classes
  • Use implicit properties for mutable, nonserializable data

You’re already a successful C# programmer–this book can help you become an outstanding one.

Read More Show Less

Product Details

  • ISBN-13: 9780321485892
  • Publisher: Addison-Wesley
  • Publication date: 10/21/2008
  • Series: Effective Software Development Series
  • Edition description: Original
  • Pages: 297
  • Sales rank: 988,262
  • Product dimensions: 6.90 (w) x 9.10 (h) x 0.90 (d)

Meet the Author

Bill Wagner, cofounder of SRT Solutions, has developed commercial software for more than twenty years and led design for many successful engineering and enterprise Microsoft Windows products. He has been a Microsoft Regional Director since 2003 and a Microsoft MVP for C# since 2005. Wagner consults routinely with the C# team on new features for forthcoming versions of C#. He has a regular column in Visual Studio Magazine and speaks frequently at conferences and user groups. His tutorials and advanced essays have appeared in MSDN Magazine, MSDN Online, .NET Insight, .NET DJ, and the MSDN C# Team Developer Center. Wagner is also the author of the best-selling Effective C# (Addison-Wesley, 2005).

Read More Show Less

Read an Excerpt

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 are an 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.

Read More Show Less

Table of Contents

Introduction xiii

Chapter 1: Working with Generics 1

Item 1: Use Generic Replacements of 1.x Framework API Classes 4

Item 2: Define Constraints That Are Minimal and Sufficient 14

Item 3: Specialize Generic Algorithms Using Runtime Type Checking 19

Item 4: Use Generics to Force Compile-Time Type Inference 26

Item 5: Ensure That Your Generic Classes Support Disposable Type Parameters 32

Item 6: Use Delegates to Define Method Constraints on Type Parameters 36

Item 7: Do Not Create Generic Specialization on Base Classes or Interfaces 42

Item 8: Prefer Generic Methods Unless Type Parameters Are Instance Fields 46

Item 9: Prefer Generic Tuples to Output and Ref Parameters 50

Item 10: Implement Classic Interfaces in Addition to Generic Interfaces 56

Chapter 2: Multithreading in C# 63

Item 11: Use the Thread Pool Instead of Creating Threads 67

Item 12: Use BackgroundWorker for Cross-Thread Communication 74

Item 13: Use lock() as Your First Choice for Synchronization 78

Item 14: Use the Smallest Possible Scope for Lock Handles 86

Item 15: Avoid Calling Unknown Code in Locked Sections 90

Item 16: Understand Cross-Thread Calls in Windows Forms and WPF 93

Chapter 3: C# Design Practices 105

Item 17: Create Composable APIs for Sequences 105

Item 18: Decouple Iterations from Actions, Predicates, and Functions 112

Item 19: Generate Sequence Items as Requested 117

Item 20: Loosen Coupling by Using Function Parameters 120

Item 21: Create Method Groups That Are Clear, Minimal, and Complete 127

Item 22: Prefer Defining Methods to Overloading Operators 134

Item 23: Understand How Events Increase Runtime Coupling Among Objects 137

Item 24: Declare Only Nonvirtual Events 139

Item 25: Use Exceptions to Report Method Contract Failures 146

Item 26: Ensure That Properties Behave Like Data 150

Item 27: Distinguish Between Inheritance and Composition 156

Chapter 4: C# 3.0 Language Enhancements 163

Item 28: Augment Minimal Interface Contracts with Extension Methods 163

Item 29: Enhance Constructed Types with Extension Methods 167

Item 30: Prefer Implicitly Typed Local Variables 169

Item 31: Limit Type Scope by Using Anonymous Types 176

Item 32: Create Composable APIs for External Components 180

Item 33: Avoid Modifying Bound Variables 185

Item 34: Define Local Functions on Anonymous Types 191

Item 35: Never Overload Extension Methods 196

Chapter 5: Working with LINQ 201

Item 36: Understand How Query Expressions Map to Method Calls 201

Item 37: Prefer Lazy Evaluation Queries 213

Item 38: Prefer Lambda Expressions to Methods 218

Item 39: Avoid Throwing Exceptions in Functions and Actions 222

Item 40: Distinguish Early from Deferred Execution 225

Item 41: Avoid Capturing Expensive Resources 229

Item 42: Distinguish Between IEnumerable and IQueryable Data Sources 242

Item 43: Use Single() and First() to Enforce Semantic Expectations on Queries 247

Item 44: Prefer Storing Expression<> to Func<> 249

Chapter 6: Miscellaneous 255

Item 45: Minimize the Visibility of Nullable Values 255

Item 46: Give Partial Classes Partial Methods for Constructors, Mutators, and Event Handlers 261

Item 47: Limit Array Parameters to Params Arrays 266

Item 48: Avoid Calling Virtual Functions in Constructors 271

Item 49: Consider Weak References for Large Objects 274

Item 50: Prefer Implicit Properties for Mutable, Nonserializable Data 277

Index 283

Read More Show Less

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 are an 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.

Read More Show Less

Customer Reviews

Be the first to write a review
( 0 )
Rating Distribution

5 Star

(0)

4 Star

(0)

3 Star

(0)

2 Star

(0)

1 Star

(0)

Your Rating:

Your Name: Create a Pen Name or

Barnes & Noble.com Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & Noble.com that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & Noble.com does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at BN.com or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation

Reminder:

  • - By submitting a review, you grant to Barnes & Noble.com and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Noble.com Terms of Use.
  • - Barnes & Noble.com reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & Noble.com also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on BN.com. It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

 
Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously

    If you find inappropriate content, please report it to Barnes & Noble
    Why is this product inappropriate?
    Comments (optional)