C# 6.0 Cookbook
by Jay Hilyard and Stephen Teilhet
Copyright 2015 Jay Hilyard, Stephen Teilhet. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Editor: Brian MacDonald
- Production Editor: Nicholas Adams
- Copyeditor: Rachel Monaghan
- Proofreader: Kim Cofer
- Indexer: Judith McConville
- Interior Designer: David Futato
- Cover Designer: Ellie Volckhausen
- Illustrator: Rebecca Demarest
- January 2004: First Edition
- January 2006: Second Edition
- December 2007: Third Edition
- October 2015: Fourth Edition
Revision History for the Fourth Edition
- 2015-09-28: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781491921463 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. C# 6.0 Cookbook, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-4919-2146-3
[LSI]
To Seth, Tom, Katie, and Jenna.
Thank you for your friendship.
Our lives are richer, happier, and more filled with laughter
for having you in them.
Jay Hilyard
To my dear friend, pastor, and teacher, Damon Thomas.
You and your family have been such a blessing to ours.
Stephen Teilhet
Preface
C# is a language targeted at developers for the Microsoft .NET platform. Microsoft portrays C# as a modern and innovative language for .NET development and continues to deliver on that in C# 6.0 with features that help support dynamic programming, parallel programming, and writing less code. C# still allows for both declarative and functional styles of programming, and still includes great object-oriented features as well. In short, C# allows you to use the style of programming that fits your particular problem.
We started writing this book together based on programming problems we ran into when we were first learning C#, and we have continued to expand it based on new challenges and capabilities in the language. In this edition, we have reworked the approach of many solutions to take advantage of the latest innovations in C# like the new expression-level (nameof
, string interpolation, null
conditional, index initializers), member declaration (auto-property initializers, getter-only auto-properties, expression-bodied function members), and statement-level (exception filters) features. We have also incorporated new uses of dynamic programming (C# 4.0) and asynchronous programming (C# 5.0) into both existing and new recipes to help you understand how to use these language features.
We hope that these additions will help you get past some of the common (and not-so-common) pitfalls and questions everyone has when learning C# for the first time, exploring a new capacity of the language, or working on the slightly off-the-beaten-path items that come up during a development cycle. There are recipes addressing things we found missing from the .NET Framework Class Library (FCL), even though Microsoft has provided tons of functionality to keep folks from reinventing the wheel. Some of these solutions you might immediately use, and some may never darken your door, but we hope this book helps you get the most out of C# and the .NET Framework.
The book is laid out with respect to the types of problems you will solve as you progress through your life as a C# programmer. These solutions are called recipes; each recipe consists of a single problem, its solution, a discussion of the solution and other relevant related information, and finally, a list of resources such as where in the FCL you can find more information about the classes used, other books addressing the topic, related articles, and other recipes. The question/answer format provides complete solutions to problems, making the book easy to read and use. Nearly every recipe contains a complete, documented code sample, showing you how to solve the specific problem, as well as a discussion of how the underlying technology works and a list of alternatives, limitations, and other considerations when appropriate.
Who This Book Is For
You dont have to be an experienced C# or .NET developer to use this book it is designed for users of all levels. This book provides solutions to problems that developers face every day as well as some that may come along less frequently. The recipes are targeted at the real-world developer who needs to solve problems now, not learn lots of theory first. While reference or tutorial books can teach general concepts, they do not generally provide the help you need in solving real-world problems. We choose to teach by example, the natural way for most people to learn.
The majority of the problems addressed in this book are frequently faced by C# developers, but some of the more advanced problems call for more intricate solutions that combine many techniques. Each recipe is designed to help you quickly understand the problem, learn how to solve it, and find out any potential trade-offs or ramifications to help you solve your problems quickly, efficiently, and with minimal effort.
To save you even the effort of typing in the solution, we provide the sample code for the book on the OReilly website to facilitate the editor inheritance mode of development (copy and paste) as well as to help less experienced developers see good programming practice in action. The sample code provides a running test harness that exercises each of the solutions, but the book includes enough of the code in each solution to allow you to implement the solution without the sample code. The sample code is available from the books product page (https://github.com/oreillymedia/c_sharp_6_cookbook).
What You Need to Use This Book
To run the samples in this book, you need a computer running Windows 7 or later. A few of the networking and XML solutions require Microsoft Internet Information Server (IIS) version 7.5 or later, and the FTP recipes in
To open and compile the samples in this book, you need Visual Studio 2015. If you are proficient with the downloadable Framework SDK and its command-line compilers, you should not have any trouble following the text of this book and the code samples.
Platform Notes
The solutions in this book were developed using Visual Studio 2015. The differences between C# 6.0 and C# 3.0 are significant, and the sample code has changed from the third edition to reflect that.