C# 7.0 Pocket Reference Instant Help for C# 7.0 Programmers Joseph Albahari and Ben Albahari C# 7.0 Pocket Reference by Joseph Albahari and Ben Albahari Copyright © 2017 Joseph Albahari, Ben Albahari. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebasto‐ pol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promo‐ tional use. Online editions are also available for most titles (http://oreilly.com/ safari). For more information, contact our corporate/institutional sales department: 800-998-9938 or [email protected]. Editor: Rachel Roumeliotis Production Editor: Nicholas Adams Copyeditor: Rachel Monaghan Proofreader: Octal Publishing, Inc. Indexer: WordCo Indexing Services Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: Rebecca Demarest July 2017: First Edition Revision History for the First Edition 2017-07-03: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781491988534 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. C# 7.0 Pocket Reference, the cover image, and related trade dress are trademarks of O’Reilly 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 intellec‐ tual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-491-98853-4 [M] Table of Contents C# 7.0 Pocket Reference 1 Conventions Used in This Book 2 Using Code Examples 3 O’Reilly Safari 3 How to Contact Us 4 A First C# Program 5 Syntax 8 Type Basics 11 Numeric Types 20 Boolean Type and Operators 28 Strings and Characters 29 Arrays 34 Variables and Parameters 38 Expressions and Operators 46 Null Operators 52 Statements 54 Namespaces 63 Classes 67 Inheritance 82 iii The object Type 91 Structs 95 Access Modifiers 96 Interfaces 98 Enums 101 Nested Types 104 Generics 105 Delegates 114 Events 120 Lambda Expressions 126 Anonymous Methods 131 try Statements and Exceptions 132 Enumeration and Iterators 140 Nullable Types 146 Extension Methods 151 Anonymous Types 153 Tuples (C# 7) 154 LINQ 156 Dynamic Binding 182 Operator Overloading 191 Attributes 194 Caller Info Attributes 198 Asynchronous Functions 199 Unsafe Code and Pointers 209 Preprocessor Directives 213 XML Documentation 215 Index 219 iv | Table of Contents C# 7.0 Pocket Reference C# is a general-purpose, type-safe, object-oriented program‐ ming language. The goal of the language is programmer pro‐ ductivity. To this end, the language balances simplicity, expres‐ siveness, and performance. The C# language is platform- neutral, but it was written to work well with the Micro‐ soft .NET Framework. C# 7.0 targets .NET Framework 4.6/4.7. NOTE The programs and code snippets in this book mirror those in Chapters 2 through 4 of C# 7.0 in a Nutshell and are all available as interactive samples in LINQPad. Working through these samples in conjunction with the book accel‐ erates learning in that you can edit the samples and instantly see the results without needing to set up projects and solutions in Visual Studio. To download the samples, click the Samples tab in LINQ‐ Pad and click “Download more samples.” LINQPad is free —go to http://www.linqpad.net. 1 Conventions Used in This Book The following typographical conventions are used in this book: Italic Indicates new terms, URLs, email addresses, filenames, and file extensions. Constant width Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, state‐ ments, and keywords. Constant width bold Shows commands or other text that should be typed liter‐ ally by the user. Constant width italic Shows text that should be replaced with user-supplied val‐ ues or by values determined by context. TIP This element signifies a tip or suggestion. NOTE This element signifies a general note. WARNING This element indicates a warning or caution. 2 | C# 7.0 Pocket Reference Using Code Examples The programs and code snippets in this book are all available as interactive samples in LINQPad. To download the samples, go to http://bit.ly/linqpad_csharp7_samples. This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permis‐ sion. Incorporating a significant amount of example code from this book into your product’s documentation does require permission. We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “C# 7.0 Pocket Reference by Joseph Albahari and Ben Albahari (O’Reilly). Copyright 2017 Joseph Albahari, Ben Albahari, 978-1-491-98853-4.” If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at [email protected]. O’Reilly Safari Safari (formerly Safari Books Online) is a membership-based training and reference platform for enterprise, government, educators, and individuals. Using Code Examples | 3 Members have access to thousands of books, training videos, Learning Paths, interactive tutorials, and curated playlists from over 250 publishers, including O’Reilly Media, Harvard Busi‐ ness Review, Prentice Hall Professional, Addison-Wesley Pro‐ fessional, Microsoft Press, Sams, Que, Peachpit Press, Adobe, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apr‐ ess, Manning, New Riders, McGraw-Hill, Jones & Bartlett, and Course Technology, among others. For more information, please visit http://oreilly.com/safari. How to Contact Us Please address comments and questions concerning this book to the publisher: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 800-998-9938 (in the United States or Canada) 707-829-0515 (international or local) 707-829-0104 (fax) We have a web page for this book, where we list errata, exam‐ ples, and any additional information. You can access this page at http://bit.ly/csharp_7_pocketref. To comment or ask technical questions about this book, send email to [email protected]. For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com. Find us on Facebook: http://facebook.com/oreilly Follow us on Twitter: http://twitter.com/oreillymedia Watch us on YouTube: http://www.youtube.com/oreillymedia 4 | C# 7.0 Pocket Reference