Understanding .NET Core
Total Page:16
File Type:pdf, Size:1020Kb
C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development Fourth Edition Build applications with C#, .NET Core, Entity Framework Core, ASP.NET Core, and ML.NET using Visual Studio Code Mark J. Price BIRMINGHAM - MUMBAI C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development Fourth Edition Copyright © 2019 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Acquisition Editor: Ben Renow-Clarke Acquisition Editor – Peer Reviews: Suresh Jain Content Development Editor: Ian Hough Project Editor: Radhika Atitkar Technical Editor: Aniket Shetty Proofreader: Safs Editing Indexer: Rekha Nair Presentation Designer: Sandip Tadge First published: March 2016 Second edition: March 2017 Third edition: November 2017 Fourth edition: October 2019 Production reference: 3311219 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78847-812-0 www.packt.com packt.com Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? • Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals • Learn better with Skill Plans built especially for you • Get a free eBook or video every month • Fully searchable for easy access to vital information • Copy and paste, print, and bookmark content Did you know that Packt offers eBook versions of every book published, with PDF and ePub fles available? You can upgrade to the eBook version at www.Packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.Packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. Contributors About the author Mark J. Price is a Microsoft Specialist: Programming in C# and Architecting Microsoft Azure Solutions, with more than 20 years of educational and programming experience. Since 1993, Mark has passed more than 80 Microsoft programming exams and specializes in preparing others to pass them too. His students range from professionals with decades of experience to 16-year-old apprentices with none. He successfully guides all of them by combining educational skills with real-world experience in consulting and developing systems for enterprises worldwide. Between 2001 and 2003, Mark was employed full-time to write offcial courseware for Microsoft in Redmond, USA. His team wrote the frst training courses for C# while it was still an early alpha version. While with Microsoft, he taught "train-the-trainer" classes to get Microsoft Certifed Trainers up to speed on C# and .NET. Currently, Mark creates and delivers training courses for Episerver's Digital Experience Platform, the best .NET CMS for Digital Marketing and E-commerce. In 2010, Mark studied for a Postgraduate Certifcate in Education (PGCE). He taught GCSE and A-Level mathematics in two London secondary schools. He holds a Computer Science BSc. Hons. degree from the University of Bristol, UK. Thank you to my parents, Pamela and Ian, for raising me to be polite, hardworking, and curious about the world. Thank you to my sisters, Emily and Juliet, for loving me despite being their awkward older brother. Thank you to my friends and colleagues who inspire me technically and creatively. Lastly, thanks to all the students I have taught over the years for motivating me to be the best teacher that I can be. About the reviewer Damir Arh has many years of experience with software development and maintenance; from complex enterprise software projects to modern consumer oriented mobile applications. Although he has worked with a wide spectrum of different languages, his favorite language remains C#. In his drive towards better development processes, he is a proponent of test-driven development, continuous integration, and continuous deployment. He shares his knowledge by speaking at local user groups and conferences, blogging, and writing articles. He has received the prestigious Microsoft MVP award for developer technologies 7 times in a row. In his spare time, he's always on the move: hiking, geocaching, running, and rock climbing. I'd like to thank my family and friends for their patience and understanding during the weekends and evenings I spent on my computer to help make this book better for everyone. Table of Contents Preface xxi Chapter 1: Hello, C#! Welcome, .NET! 1 Setting up your development environment 2 Using Visual Studio Code for cross-platform development 2 Using Visual Studio 2019 for Windows app development 3 Using Visual Studio for Mac for mobile development 3 Recommended tools for chapters 3 Deploying cross-platform 4 Understanding Microsoft Visual Studio Code versions 4 Downloading and installing Visual Studio Code 6 Installing other extensions 7 Understanding .NET 7 Understanding the .NET Framework 7 Understanding the Mono and Xamarin projects 8 Understanding .NET Core 8 Understanding future versions of .NET 9 Understanding .NET Core support 10 What is diferent about .NET Core? 11 Understanding .NET Standard 12 .NET platforms and tools used by the book editions 13 Understanding intermediate language 13 Understanding .NET Native 14 Comparing .NET technologies 14 Building console apps using Visual Studio Code 15 Writing code using Visual Studio Code 15 Compiling and running code using dotnet CLI 17 Downloading solution code from a GitHub repository 17 Using Git with Visual Studio Code 18 Cloning the book solution code repository 18 Looking for help 18 Reading Microsoft documentation 19 Getting help for the dotnet tool 19 Getting defnitions of types and their members 19 Looking for answers on Stack Overfow 21 Searching for answers using Google 22 Subscribing to the ofcial .NET blog 23 [ i ] Table of Contents Practicing and exploring 23 Exercise 1.1 – Test your knowledge 23 Exercise 1.2 – Practice C# anywhere 24 Exercise 1.3 – Explore topics 24 Summary 24 Chapter 2: Speaking C# 25 Introducing C# 25 Understanding language versions and features 25 C# 1.0 26 C# 2.0 26 C# 3.0 26 C# 4.0 26 C# 5.0 27 C# 6.0 27 C# 7.0 27 C# 7.1 28 C# 7.2 28 C# 7.3 28 C# 8.0 28 Discovering your C# compiler versions 29 Enabling a specifc language version compiler 30 Understanding C# basics 31 Understanding C# grammar 32 Statements 32 Comments 32 Blocks 33 Understanding C# vocabulary 33 Help for writing correct code 34 Verbs are methods 35 Nouns are types, felds, and variables 36 Revealing the extent of the C# vocabulary 36 Working with variables 38 Naming things and assigning values 39 Literal values 39 Storing text 40 Understanding verbatim strings 40 Storing numbers 41 Storing whole numbers 42 Storing real numbers 43 Writing code to explore number sizes 44 Comparing double and decimal types 45 Storing Booleans 47 Using Visual Studio Code workspaces 47 Storing any type of object 48 Storing dynamic types 49 [ ii ] Table of Contents Declaring local variables 50 Specifying and inferring the type of a local variable 50 Getting default values for types 51 Storing multiple values 52 Working with null values 53 Making a value type nullable 53 Understanding nullable reference types 54 Enabling nullable and non-nullable reference types 55 Declaring non-nullable variables and parameters 55 Checking for null 57 Exploring console applications further 58 Displaying output to the user 59 Formatting using numbered positional arguments 59 Formatting using interpolated strings 59 Understanding format strings 60 Getting text input from the user 61 Importing a namespace 62 Simplifying the usage of the console 62 Getting key input from the user 63 Getting arguments 63 Setting options with arguments 65 Handling platforms that do not support an API 66 Practicing and exploring 67 Exercise 2.1 – Test your knowledge 67 Exercise 2.2 – Practice number sizes and ranges 68 Exercise 2.3 – Explore topics 69 Summary 69 Chapter 3: Controlling Flow and Converting Types 71 Operating on variables 71 Unary operators 72 Binary arithmetic operators 73 Assignment operators 74 Logical operators 75 Conditional logical operators 76 Bitwise and binary shift operators 77 Miscellaneous operators 79 Understanding selection statements 79 Branching with the if statement 79 Why you should always use braces with if statements 81 Pattern matching with the if statement 81 Branching with the switch statement 82 [ iii ] Table of Contents Pattern matching with the switch statement 84 Simplifying switch statements with