Professional C# 6 and .NET Core 1.0 Christian Nagel Professional C# 6 and .NET Core 1.0 Published by John Wiley & Sons, I nc. 10475 Crosspoint Boulevard I ndianapolis, I N 46256 w w w .w iley.com Copyright © 2016 by John Wiley & Sons, I nc., I ndianapolis, I ndiana Published sim ultaneously in Canada I SBN: 978-1-119-09660-3 I SBN: 978-1-119-09671-9 (ebk) I SBN: 978-1-119-09663-4 (ebk) Manufactured in the U nited States of Am erica No part of this publication m ay be reproduced, stored in a retrieval system or transm itted in any form or by any m eans, electronic, m echanical, photocopying, recording, scanning or otherw ise, except as perm itted under Sections 107 or 108 of the 1976 United States Copyright Act, w ithout either the prior w ritten perm ission of the Publisher, or authorization through paym ent of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosew ood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for perm ission should be addressed to the Perm issions Department, John Wiley & Sons, I nc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://w w w .w iley.com /go/perm issions. Limit of Liability/Disclaimer of Warranty: The publisher and the author m ake no representations or w arranties w ith respect to the accuracy or com pleteness of the contents of this w ork and specifically disclaim all w arranties, including w ithout lim itation w arranties of fitness for a particular purpose. No w arranty m ay be created or extended by sales or prom otional m aterials. The advice and strategies contained herein m ay not be suitable for every situation. This w ork is sold w ith the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. I f professional assistance is required, the services of a com petent professional person should be sought. Neither the publisher nor the author shall be liable for dam ages arising herefrom. The fact that an organization or Web site is referred to in this w ork as a citation and/or a potential source of further inform ation does not m ean that the author or the publisher endorses the inform ation the organization or Web site m ay provide or recom m endations it m ay m ake. Further, readers should be aw are that I nternet Web sites listed in this w ork may have changed or disappeared betw een w hen this w ork w as w ritten and w hen it is read. For general inform ation on our other products and services please contact our Custom er Care Departm ent w ithin the United States at (877) 762-2974, outside the United States at (317) 572- 3993 or fax (317) 572-4002. Wiley publishes in a variety of print and electronic form ats and by print-on-dem and. Som e m aterial included w ith standard print versions of this book m ay not be included in e-books or in print-on-dem and. I f this book refers to m edia such as a CD or DVD that is not included in the version you purchased, you m ay dow nload this m aterial at http://booksupport.w iley.com . For m ore information about Wiley products, visit w w w .w iley.com . Library of Congress Control Number: 2016932153 Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Program m er to Program m er, and related trade dress are tradem arks or registered trademarks of John Wiley & Sons, I nc. and/or its affiliates, in the United States and other countries, and m ay not be used w ithout w ritten perm ission. All other tradem arks are the property of their respective ow ners. John Wiley & Sons, I nc., is not associated w ith any product or vendor m entioned in this book. This book is dedicated to my family—Angela, Stephanie, and Matthias—I love you all! About the Author CHRISTIAN NAGEL is Microsoft MVP for Visual Studio and Development Technologies, and has been Microsoft Regional Director for more than 15 years. Christian is an associate of thinktecture and founder of CN innovation, where he offers training and consulting on how to develop solutions using the Microsoft platform. He draws on more than 25 years of software development experience. Christian started his computing career with PDP 11 and VAX/VMS systems at Digital Equipment Corporation, covering a variety of languages and platforms. Since 2000, when .NET was just a technology preview, he has been working with various technologies to build .NET solutions. Currently, he mainly coaches people on development of Universal Windows Platform apps and ASP.NET MVC, using several Microsoft Azure service offerings. Even after many years in software development, Christian still loves learning and using new technologies and teaching others how to use the new technologies in various forms. Using his profound knowledge of Microsoft technologies, he has written numerous books, and is certified as Microsoft Certified Trainer and Certified Solution Developer. Christian speaks at international conferences such as TechEd, BASTA!, and TechDays. He founded INETA Europe to support .NET user groups. You can contact Christian via his website www.cninnovation.com and follow his tweets at @christiannagel. About the Technical Editor ISTVÁN NOVÁK is an associate and the chief technology consultant with SoftwArt, a small Hungarian IT consulting company. He works as a software architect and community evangelist. In the last 25 years, he has participated in more than 50 enterprise software development projects. In 2002, he coauthored the first Hungarian book about .NET development. In 2007, he was awarded the Microsoft Most Valuable Professional (MVP) title, and in 2011 he became a Microsoft Regional Director. István coauthored Visual Studio 2010 and .NET 4 Six-in-One (Wiley, 2010) and Beginning Windows 8 Application Development (Wiley, 2012), and he authored Beginning Visual Studio LightSwitch Development (Wiley, 2011). István holds master’s degree from the Technical University of Budapest, Hungary and also has a doctoral degree in software technology. He lives in Dunakeszi, Hungary, with his wife and two daughters. He is a passionate scuba diver. You may have a good chance of meeting him underwater in the Red Sea, any season of the year. Credits SENIOR ACQUISITIONS EDITOR Ken Brown PROJECT EDITOR Charlotte Kughen TECHNICAL EDITOR István Novák PRODUCTION EDITOR Dassi Zeidel MANAGER OF CONTENT DEVELOPMENT & ASSEMBLY Mary Beth Wakefield PRODUCTION MANAGER Kathleen Wisor MARKETING DIRECTOR David Mayhew MARKETING MANAGER Carrie Sherrill PROFESSIONAL TECHNOLOGY & STRATEGY DIRECTOR Barry Pruett BUSINESS MANAGER Amy Knies ASSOCIATE PUBLISHER Jim Minatel PROJECT COORDINATOR, COVER Patrick Redmond PROOFREADER Amy J. Schneider INDEXER John Sleeva COVER DESIGNER Wiley COVER IMAGE © Digital Storn/Shutterstock Acknowledgments I WANT TO THANK Charlotte Kughen, who made my text so much more readable. Often I was working late at night writing while .NET Core was continuously evolving. Charlotte was of enormous help to change my ideas into great readable text. I’m almost sure that Charlotte now knows a lot more about programming than she would like to. Special thanks also goes to István Novák, who has authored several great books. Despite all the issues we had with the fast-evolving .NET Core and the interim builds I was using while working on the book, István challenged me to enhance the code samples that allow you—the reader—to better follow the flow. Thank you, Charlotte and István— you’ve been of great help for the quality of this book. I also would like to thank Kenyon Brown and Jim Minatel and everyone else at Wiley who helped to get edition 10 of this great book to be published. I also want to thank my wife and children for supporting my writing. You understood and helped with the time I was working on the book, including evenings, nights, and weekends. Angela, Stephanie, and Matthias—you are my loved ones. This would not have been possible without you. CONTENTS Introduction The Significance of .NET Core The Significance of C# What’s New in C# 6 What’s New with the Universal Windows Platform What You Need to Write and Run C# Code What This Book Covers Conventions Source Code Errata p2p.wrox.com Part I: The C# Language Chapter 1: .NET Application Architectures Choosing Your Technologies Reviewing .NET History .NET 2015 Hello, World Compiling with .NET 4.6 Compiling with .NET Core CLI Application Types and Technologies Developer Tools Summary Chapter 2: Core C# Fundamentals of C# Creating Hello, World! with Visual Studio Working with Variables Using Predefined Data Types Controlling Program Flow Working with Enumerations Getting Organized with Namespaces Understanding the Main Method Using Comments Understanding C# Preprocessor Directives C# Programming Guidelines Summary Chapter 3: Objects and Types Creating and Using Classes Classes and Structs Classes Anonymous Types Structs Passing Parameters by Value and by Reference Nullable Types Enumerations Partial Classes Extension Methods The Object Class Summary Chapter 4: Inheritance Inheritance Types of Inheritance Implementation Inheritance Modifiers Interfaces is and as Operators Summary Chapter 5: Managed and Unmanaged Resources Resources Memory Management Under the Hood Strong and Weak References Working with Unmanaged Resources Unsafe Code Platform Invoke Summary Chapter 6: Generics Generics Overview Creating Generic Classes Generics Features Generic Interfaces Generic Structs Generic Methods Summary Chapter 7: Arrays and Tuples Multiple Objects of the Same and Different Types
Description: