ePUB is an open, industry-standard format for eBooks. However, support of ePUB and its many features varies across reading devices and applications. Use your device or app settings to customize the presentation to your liking. Settings that you can customize often include font, font size, single or double column, landscape or portrait mode, and figures that you can click or tap to enlarge. For additional information about the settings and features on your reading device or app, visit the device manufacturers Web site.
Many titles include programming code or configuration examples. To optimize the presentation of these elements, view the eBook in single-column, landscape mode and adjust the font size to the smallest setting. In addition to presenting code and configurations in the reflowable text format, we have included images of the code that mimic the presentation found in the print book; therefore, where the reflowable format may compromise the presentation of the code listing, you will see a Click here to view code image link. Click the link to view the print-fidelity code image. To return to the previous page viewed, click the Back button on your device or app.
Effective C#
50 Specific Ways to Improve Your C#
Third Edition
Bill Wagner
Boston Columbus Indianapolis New York San Francisco Amsterdam Cape Town
Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City
So Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at or (800) 382-3419.
For government sales inquiries, please contact .
For questions about sales outside the U.S., please contact .
Visit us on the Web: informit.com/aw
Library of Congress Control Number: 2016953545
Copyright 2017 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms, and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/.
ISBN-13: 978-0-672-33787-1
ISBN-10: 0-672-33787-8
1 16
Praise for Effective C#, Second Edition
Being an effective .NET developer requires one to have a deep understanding of the language of their choice. Wagners book provides the reader with that knowledge via well-reasoned arguments and insight. Whether youre new to C# or youve been using it for years, youll learn something new when you read this book.
Jason Bock, Principal Consultant, Magenic
If youre at all like me, you have collected a handful of C# language pearls that have immediately transformed your abilities as a professional developer. What you hold in your hands is quite possibly the best collection of these tips that have ever been assembled. Bill has managed to exceed my wildest expectations with the latest edition in his eponymous Effective C#.
Bill Craun, Principal Consultant, Ambassador Solutions
Effective C#, Second Edition, is a must-read for anyone building high performance and/or highly scalable applications. Bill has that rare and awesome ability to take an amazingly complex problem and break it down into human, digestible, and understandable chunks.
Josh Holmes, Architect Evangelist, Microsoft
Bill has done it again. This book is a concise collection of invaluable tips for any C# developer. Learn one tip every day, and youll become a much better C# developer after fifty days!
Claudio Lassala, Lead Developer, EPS Software/CODE Magazine
A fountain of knowledge and understanding of the C# language. Bill gives insight to what happens under the covers of the .NET runtime based on what you write in your code and teaches pragmatic practices that lead to cleaner, easier to write, and more understandable code. A great mix of tips, tricks, and deep understanding... that every C# developer should read.
Brian Noyes, Chief Architect, IDesign Inc. (www.idesign.net)
Effective C# is a must-have for every C# developer. Period. Its pragmatic advice on code design is invaluable.
Shawn Wildermuth, Microsoft MVP (C#), Author, Trainer, and Speaker
In this book Bill Wagner provides practical explanations of how to use the most important features in the C# language. His deep knowledge and sophisticated communication skills illuminate the new features in C# so that you can use them to write programs that are more concise and easier to maintain.
Charlie Calvert, Microsoft C# Community Program Manager
To Marlene, who continues to provide inspiration and support for everything we do together.
Introduction
The C# community is very different in 2016 from what it was in 2004 when the first edition of Effective C# was published. There are many more developers using C#. A large contingent of the C# community is now seeing C# as their first professional language. They arent approaching C# with a set of ingrained habits formed using a different language. The community has a much broader range of experience. New graduates all the way to professionals with decades of experience are using C#. C# now runs on multiple platforms. You can build server applications, Web sites, desktop applications, and mobile applications for multiple platforms in the C# language.
I organized this third edition of Effective C# by taking into account both the changes in the language and the changes in the C# community. Effective C# does not take you on a historical journey through the changes in the language. Rather, I provide advice on how to use the current C# language. The items that have been removed from this edition are those that arent as relevant in todays C# language, or to todays applications. The new items cover the new language and framework features, and those practices the community has learned from building several versions of software products using C#. Readers of earlier editions will note that content from the first edition of More