《C# 10 and .NET 6 – Modern Cross-Platform Development原版完整文件.docx》由会员分享,可在线阅读,更多相关《C# 10 and .NET 6 – Modern Cross-Platform Development原版完整文件.docx(825页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、C# 10 and .NET 6 Modern Cross-Platform DevelopmentSixth EditionBuild apps, websites, and services with ASP.NET Core 6, Blazor, and EF Core 6 using Visual Studio 2022 and Visual Studio CodeMark J. PriceBIRMINGHAMMUMBAIC# 10 and .NET 6 Modern Cross-Platform DevelopmentSixth EditionCopyright 2021 Packt
2、 PublishingAll 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 be
3、en 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 dama
4、ges 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
5、 this information.Producer: Suman SenAcquisition Editor Peer Reviews: Saby DsilvaProject Editor: Amit RamadasContent Development Editor: Bhavesh AminCopy Editor: Safis Editing Technical Editor: Aniket Shetty Proofreader: Safis Editing Indexer: Pratik ShirodkarPresentation Designer: Pranit PadwalFirs
6、t published: March 2016 Second edition: March 2017 Third edition: November 2017 Fourth edition: October 2019 Fifth edition: November 2020 Sixth edition: November 2021 Production reference: 1021121Published by Packt Publishing Ltd. Livery Place35 Livery Street Birmingham B3 2PB, UK.ISBN 978-1-80107-7
7、36-1ContributorsAbout the authorMark J. Price is a Microsoft Specialist: Programming in C# and Architecting Microsoft Azure Solutions, with over 20 years experience.Since 1993, he has passed more than 80 Microsoft programming exams and specializes in preparing others to pass them. Between 2001 and 2
8、003, Mark was employed to write official courseware for Microsoft in Redmond, USA. His team wrote the first training courses for C# while it was still an early alpha version. While with Microsoft, he taught train-the-trainer classes to get other MCTs up to speed on C# and .NET. Currently, Mark creat
9、es and delivers training courses for Optimizelys Digital Experience Platform (DXP). Mark holds a BSc. Hons. Degree in computer science.About the reviewersDamir Arh has many years of experience with software development and maintenance; from complex enterprise software projects to modern consumer-ori
10、ented mobile applications. Although he has worked with a wide spectrum of different languages, his favorite language remains C#. In his drive toward better development processes, he is a proponent of test-drivendevelopment, continuous integration, and continuous deployment. He shares his knowledge b
11、y speaking at local user groups and conferences, blogging, and writing articles. He has received the prestigious Microsoft MVP award for developer technologies 10 times in a row. In his spare time, hes always on the move: hiking, geocaching, running, and rock climbing.Geovanny Alzate Sandoval is a s
12、ystem engineer from Medelln, Colombia, and enjoys everything related to software development, new technologies, design patterns, and software architecture. He has 14+ years of experience working as a developer, technical leader, and software architect mostly with Microsoft technologies. He loves con
13、tributing to OSS, he has made contributions to Asp.Net Core SignalR, Polly, and Apollo Server to mention a few. Hes also the co-author of Simmy, an OSS library for chaos engineering for .NET based on Polly.Hes also a DDD lover and a cloud enthusiast. In addition, hes a .Net Foundation member and a c
14、o-organizer of MDE.NET community, which is a community for .NET developers in Medelln/Colombia. In recent years, he has been focused on building distributed and reliable systems using distributed architectures and cloud technologies. Last but not least, he stronglybelieves in teamwork, as he says: I
15、 wouldnt be here if I wouldnt have learned that much from all the talented people Ive worked with.Geovanny currently works for Curbit, which is a US startup based in California, as Director of Engineering.Table of Contents v PrefacexxvChapter 1: Hello, C#! Welcome, .NET!1Setting up your development
16、environment2Choosing the appropriate tool and application type for learning3Pros and cons of the .NET Interactive Notebooks extension3Using Visual Studio Code for cross-platform development4Using GitHub Codespaces for development in the cloud4Using Visual Studio for Mac for general development4Using
17、 Visual Studio for Windows for general development5What I used5Deploying cross-platform6Downloading and installing Visual Studio 2022 for Windows6Microsoft Visual Studio for Windows keyboard shortcuts7Downloading and installing Visual Studio Code7Installing other extensions8Understanding Microsoft V
18、isual Studio Code versions9Microsoft Visual Studio Code keyboard shortcuts9Understanding .NET10Understanding .NET Framework10Understanding the Mono, Xamarin, and Unity projects10Understanding .NET Core11Understanding the journey to one .NET11Understanding .NET support12Understanding .NET Runtime and
19、 .NET SDK versions13Removing old versions of .NET14What is different about modern .NET?14Windows development14Web development15Database development15Themes of modern .NET15Understanding .NET Standard15.NET platforms and tools used by the book editions16Understanding intermediate language17Comparing
20、.NET technologies17Building console apps using Visual Studio 202218Managing multiple projects using Visual Studio 202218Writing code using Visual Studio 202218Compiling and running code using Visual Studio20Understanding the compiler-generated folders and files21Writing top-level programs21Adding a
21、second project using Visual Studio 202222Implicitly imported namespaces22Building console apps using Visual Studio Code24Managing multiple projects using Visual Studio Code24Writing code using Visual Studio Code24Compiling and running code using the dotnet CLI27Adding a second project using Visual S
22、tudio Code27Managing multiple files using Visual Studio Code29Exploring code using .NET Interactive Notebooks29Creating a notebook30Writing and running code in a notebook31Saving a notebook32Adding Markdown and special commands to a notebook32Executing code in multiple cells33Using .NET Interactive
23、Notebooks for the code in this book34Reviewing the folders and files for projects34Understanding the common folders and files35Understanding the solution code on GitHub36Making good use of the GitHub repository for this book36Raising issues with the book36Giving me feedback37Downloading solution cod
24、e from the GitHub repository37Using Git with Visual Studio Code and the command line38Cloning the book solution code repository38Looking for help39Reading Microsoft documentation39Getting help for the dotnet tool39Getting definitions of types and their members40Looking for answers on Stack Overflow4
25、2Searching for answers using Google43Subscribing to the official .NET blog43Watching Scott Hanselmans videos43Practicing and exploring43Exercise 1.1 Test your knowledge43Exercise 1.2 Practice C# anywhere44Exercise 1.3 Explore topics44Summary45Chapter 2: Speaking C#47Introducing the C# language47Unde
26、rstanding language versions and features47C# 1.048C# 2.048C# 3.048C# 4.048C# 5.049C# 6.049C# 7.049C# 7.149C# 7.250C# 7.350C# 850C# 950C# 1050Understanding C# standards51Discovering your C# compiler versions51How to output the SDK version52Enabling a specific language version compiler52Understanding
27、C# grammar and vocabulary53Showing the compiler version53Understanding C# grammar55Statements55Comments55Blocks56Examples of statements and blocks56Understanding C# vocabulary57Comparing programming languages to human languages57Changing the color scheme for C# syntax57Help for writing correct code5
28、8Importing namespaces59Implicitly and globally importing namespaces59Verbs are methods62Nouns are types, variables, fields, and properties62Revealing the extent of the C# vocabulary63Working with variables65Naming things and assigning values66Literal values66Storing text66Understanding verbatim stri
29、ngs67Storing numbers68Storing whole numbers68Exploring whole numbers69Storing real numbers70Writing code to explore number sizes70Comparing double and decimal types71Storing Booleans73Storing any type of object73Storing dynamic types74Declaring local variables76Specifying the type of a local variabl
30、e76Inferring the type of a local variable76Using target-typed new to instantiate objects78Getting and setting the default values for types78Storing multiple values in an array79Exploring more about console applications80Displaying output to the user81Formatting using numbered positional arguments81F
31、ormatting using interpolated strings82Understanding format strings82Getting text input from the user84Simplifying the usage of the console84Getting key input from the user85Passing arguments to a console app86Setting options with arguments88Handling platforms that do not support an API90Practicing a
32、nd exploring91Exercise 2.1 Test your knowledge91Exercise 2.2 Test your knowledge of number types92Exercise 2.3 Practice number sizes and ranges92Exercise 2.4 Explore topics93Summary93Chapter 3: Controlling Flow, Converting Types, and Handling Exceptions95Operating on variables95Exploring unary opera
33、tors96Exploring binary arithmetic operators97Assignment operators98Exploring logical operators98Exploring conditional logical operators100Exploring bitwise and binary shift operators101Miscellaneous operators103Understanding selection statements103Branching with the if statement104Why you should alw
34、ays use braces with if statements105Pattern matching with the if statement105Branching with the switch statement106Pattern matching with the switch statement108Simplifying switch statements with switch expressions109Understanding iteration statements110Looping with the while statement110Looping with
35、 the do statement111Looping with the for statement112Looping with the foreach statement112Understanding how foreach works internally113Casting and converting between types113Casting numbers implicitly and explicitly114Converting with the System.Convert type115Rounding numbers116Understanding the def
36、ault rounding rules116Taking control of rounding rules117Converting from any type to a string117Converting from a binary object to a string118Parsing from strings to numbers or dates and times119Errors using Parse120Avoiding exceptions using the TryParse method120Handling exceptions121Wrapping error
37、-prone code in a try block121Catching all exceptions123Catching specific exceptions123Catching with filters125Checking for overflow125Throwing overflow exceptions with the checked statement125Disabling compiler overflow checks with the unchecked statement127Practicing and exploring128Exercise 3.1 Te
38、st your knowledge128Exercise 3.2 Explore loops and overflow129Exercise 3.3 Practice loops and operators129Exercise 3.4 Practice exception handling130Exercise 3.5 Test your knowledge of operators130Exercise 3.6 Explore topics130Summary130Chapter 4: Writing, Debugging, and Testing Functions131Writing
39、functions131Times table example132Writing a times table function132Writing a function that returns a value134Converting numbers from cardinal to ordinal136Calculating factorials with recursion137Documenting functions with XML comments140Using lambdas in function implementations141Debugging during de
40、velopment144Creating code with a deliberate bug144Setting a breakpoint and start debugging145Using Visual Studio 2022145Using Visual Studio Code146Navigating with the debugging toolbar148Debugging windows149Stepping through code150Customizing breakpoints151Logging during development and runtime153Un
41、derstanding logging options153Instrumenting with Debug and Trace154Writing to the default trace listener154Configuring trace listeners155Switching trace levels157Adding packages to a project in Visual Studio Code157Adding packages to a project in Visual Studio 2022158Reviewing project packages158Uni
42、t testing162Understanding types of testing162Creating a class library that needs testing162Writing unit tests164Running unit tests using Visual Studio Code165Running unit tests using Visual Studio166Fix the bug166Throwing and catching exceptions in functions167Understanding usage errors and executio
43、n errors167Commonly thrown exceptions in functions167Understanding the call stack168Where to catch exceptions171Rethrowing exceptions171Implementing the tester-doer pattern173Problems with the tester-doer pattern173Practicing and exploring174Exercise 4.1 Test your knowledge174Exercise 4.2 Practice w
44、riting functions with debugging and unit testing174Exercise 4.3 Explore topics175Summary175Chapter 5: Building Your Own Types with Object-Oriented Programming177Talking about OOP177Building class libraries178Creating a class library178Defining a class in a namespace179Simplifying namespace declarati
45、ons180Understanding members181Instantiating a class181Referencing an assembly182Importing a namespace to use a type182Understanding objects183Inheriting from System.Object184Storing data within fields184Defining fields184Understanding access modifiers185Setting and outputting field values186Storing
46、a value using an enum type187Storing multiple values using an enum type188Storing multiple values using collections189Understanding generic collections190Making a field static191Making a field constant192Making a field read-only193Initializing fields with constructors194Defining multiple constructors195Writing and calling methods195Returning values from methods195Combining multiple returned values using tuples196Language support for tuples197Naming the fiel