C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development Fourth Edition 978-1-78847-812-0

1,822 345 13MB

English Pages 819 Year 2019

Report DMCA / Copyright

DOWNLOAD FILE

C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development Fourth Edition
 978-1-78847-812-0

Table of contents :
Cover......Page 1
Copyright......Page 3
packt page......Page 4
Contributors......Page 5
Table of Contents......Page 8
Preface......Page 28
Chapter 1: Hello, C#! Welcome, .NET!......Page 36
Using Visual Studio Code for cross-platform development......Page 37
Recommended tools for chapters......Page 38
Understanding Microsoft Visual Studio Code versions......Page 39
Downloading and installing Visual Studio Code......Page 41
Understanding the .NET Framework......Page 42
Understanding .NET Core......Page 43
Understanding future versions of .NET......Page 44
Understanding .NET Core support......Page 45
What is different about .NET Core?......Page 46
Understanding .NET Standard......Page 47
Understanding intermediate language......Page 48
Comparing .NET technologies......Page 49
Writing code using Visual Studio Code......Page 50
Downloading solution code from a GitHub repository......Page 52
Looking for help......Page 53
Getting definitions of types and their members......Page 54
Looking for answers on Stack Overflow......Page 56
Searching for answers using Google......Page 57
Exercise 1.1 – Test your knowledge......Page 58
Summary......Page 59
Understanding language versions and features......Page 60
C# 4.0......Page 61
C# 7.0......Page 62
C# 8.0......Page 63
Discovering your C# compiler versions......Page 64
Enabling a specific language version compiler......Page 65
Understanding C# basics......Page 66
Comments......Page 67
Understanding C# vocabulary......Page 68
Help for writing correct code......Page 69
Verbs are methods......Page 70
Revealing the extent of the C# vocabulary......Page 71
Working with variables......Page 73
Literal values......Page 74
Understanding verbatim strings......Page 75
Storing numbers......Page 76
Storing whole numbers......Page 77
Storing real numbers......Page 78
Writing code to explore number sizes......Page 79
Comparing double and decimal types......Page 80
Using Visual Studio Code workspaces......Page 82
Storing any type of object......Page 83
Storing dynamic types......Page 84
Specifying and inferring the type of a local variable......Page 85
Getting default values for types......Page 86
Storing multiple values......Page 87
Making a value type nullable......Page 88
Understanding nullable reference types......Page 89
Declaring non-nullable variables and parameters......Page 90
Checking for null......Page 92
Exploring console applications further......Page 93
Formatting using interpolated strings......Page 94
Understanding format strings......Page 95
Getting text input from the user......Page 96
Simplifying the usage of the console......Page 97
Getting arguments......Page 98
Setting options with arguments......Page 100
Handling platforms that do not support an API......Page 101
Exercise 2.1 – Test your knowledge......Page 102
Exercise 2.2 – Practice number sizes and ranges......Page 103
Summary......Page 104
Operating on variables......Page 106
Unary operators......Page 107
Binary arithmetic operators......Page 108
Assignment operators......Page 109
Logical operators......Page 110
Conditional logical operators......Page 111
Bitwise and binary shift operators......Page 112
Branching with the if statement......Page 114
Pattern matching with the if statement......Page 116
Branching with the switch statement......Page 117
Pattern matching with the switch statement......Page 119
Simplifying switch statements with switch expressions......Page 120
Looping with the while statement......Page 121
Looping with the do statement......Page 122
Looping with the foreach statement......Page 123
Casting and converting between types......Page 124
Casting numbers implicitly and explicitly......Page 125
Rounding numbers......Page 127
Understanding the default rounding rules......Page 128
Converting from any type to a string......Page 129
Converting from a binary object to a string......Page 130
Parsing from strings to numbers or dates and times......Page 131
Avoiding exceptions using the TryParse method......Page 132
Handling exceptions when converting types......Page 133
Wrapping error-prone code in a try block......Page 134
Catching specific exceptions......Page 135
Throwing overflow exceptions with the checked statement......Page 137
Disabling compiler overflow checks with the unchecked statement......Page 139
Exercise 3.2 – Explore loops and overflow......Page 140
Exercise 3.3 – Practice loops and operators......Page 141
Exercise 3.5 – Test your knowledge of operators......Page 142
Summary......Page 143
Writing functions......Page 144
Writing a times table function......Page 145
Writing a function that returns a value......Page 147
Converting numbers from ordinal to cardinal......Page 149
Calculating factorials with recursion......Page 151
Documenting functions with XML comments......Page 153
Creating code with a deliberate bug......Page 155
Setting a breakpoint......Page 156
Navigating with the debugging toolbar......Page 157
Stepping through code......Page 158
Customizing breakpoints......Page 160
Logging during development and runtime......Page 163
Instrumenting with Debug and Trace......Page 164
Configuring trace listeners......Page 165
Switching trace levels......Page 167
Unit testing functions......Page 170
Creating a class library that needs testing......Page 171
Writing unit tests......Page 172
Exercise 4.1 – Test your knowledge......Page 174
Exercise 4.3 – Explore topics......Page 175
Summary......Page 176
Talking about object-oriented programming......Page 178
Building class libraries......Page 179
Defining a class......Page 180
Understanding members......Page 181
Importing a namespace to use a type......Page 182
Understanding objects......Page 183
Inheriting from System.Object......Page 184
Understanding access modifiers......Page 185
Setting and outputting field values......Page 186
Storing a value using an enum type......Page 187
Storing multiple values using an enum type......Page 189
Storing multiple values using collections......Page 190
Making a field static......Page 191
Making a field constant......Page 193
Initializing fields with constructors......Page 194
Setting fields with default literals......Page 196
Returning values from methods......Page 197
Combining multiple returned values using tuples......Page 198
Inferring tuple names......Page 200
Defining and passing parameters to methods......Page 201
Overloading methods......Page 202
Passing optional parameters and naming arguments......Page 203
Controlling how parameters are passed......Page 205
Splitting classes using partial......Page 206
Defining readonly properties......Page 207
Defining settable properties......Page 209
Defining indexers......Page 210
Exercise 5.2 – Explore topics......Page 212
Summary......Page 213
Setting up a class library and console application......Page 214
Simplifying methods......Page 216
Implementing functionality using methods......Page 217
Implementing functionality using operators......Page 219
Implementing functionality using local functions......Page 220
Calling methods using delegates......Page 221
Defining and handling delegates......Page 223
Defining and handling events......Page 225
Common interfaces......Page 226
Comparing objects when sorting......Page 227
Comparing objects using a separate class......Page 229
Defining interfaces with default implementations......Page 230
Making types safely reusable with generics......Page 233
Working with generic types......Page 234
Working with generic methods......Page 236
Managing memory with reference and value types......Page 237
Working with struct types......Page 238
Releasing unmanaged resources......Page 240
Inheriting from classes......Page 242
Extending classes......Page 243
Hiding members......Page 244
Overriding members......Page 245
Preventing inheritance and overriding......Page 246
Understanding polymorphism......Page 247
Casting within inheritance hierarchies......Page 248
Avoiding casting exceptions......Page 249
Inheriting and extending .NET types......Page 250
Inheriting exceptions......Page 251
Using static methods to reuse functionality......Page 252
Using extension methods to reuse functionality......Page 254
Exercise 6.2 – Practice creating an inheritance hierarchy......Page 255
Exercise 6.3 – Explore topics......Page 256
Summary......Page 257
Introducing .NET Core 3.0......Page 258
.NET Core 1.1......Page 259
.NET Core 2.1......Page 260
.NET Core 3.0......Page 261
Understanding assemblies, packages, and namespaces......Page 262
Understanding the Microsoft .NET Core App platform......Page 263
Understanding frameworks......Page 265
Relating C# keywords to .NET types......Page 266
Sharing code cross-platform with .NET Standard class libraries......Page 268
Creating a console application to publish......Page 270
Creating new projects......Page 271
Managing projects......Page 272
Decompiling assemblies......Page 273
Referencing a NuGet package......Page 277
Packaging a library for NuGet......Page 278
Testing your package......Page 281
Porting from .NET Framework to .NET Core......Page 282
Should you port?......Page 283
Understanding the .NET Portability Analyzer......Page 284
Using non-.NET Standard libraries......Page 285
Exercise 7.1 – Test your knowledge......Page 286
Summary......Page 287
Working with numbers......Page 288
Working with big integers......Page 289
Working with text......Page 290
Getting the characters of a string......Page 291
Getting part of a string......Page 292
Checking a string for content......Page 293
Joining, formatting, and other string members......Page 294
Pattern matching with regular expressions......Page 295
Checking for digits entered as text......Page 296
Understanding the syntax of a regular expression......Page 297
Examples of regular expressions......Page 298
Splitting a complex comma-separated string......Page 299
Storing multiple objects in collections......Page 300
Common features of all collections......Page 301
Understanding collection choices......Page 302
Dictionaries......Page 303
Sets......Page 304
Working with lists......Page 305
Sorting collections......Page 306
Using specialized collections......Page 307
Working with spans, indexes, and ranges......Page 308
Identifying positions with the Index type......Page 309
Using indexes and ranges......Page 310
Working with network resources......Page 311
Working with URIs, DNS, and IP addresses......Page 312
Pinging a server......Page 313
Working with types and attributes......Page 314
Reading assembly metadata......Page 315
Creating custom attributes......Page 318
Doing more with reflection......Page 320
Internationalizing your code......Page 321
Exercise 8.1 – Test your knowledge......Page 323
Exercise 8.3 – Practice writing extension methods......Page 324
Summary......Page 325
Handling cross-platform environments and filesystems......Page 326
Managing drives......Page 328
Managing directories......Page 329
Managing files......Page 332
Getting file information......Page 334
Reading and writing with streams......Page 336
Writing to text streams......Page 338
Writing to XML streams......Page 340
Disposing of file resources......Page 341
Compressing streams......Page 344
Compressing with the Brotli algorithm......Page 345
High-performance streams using pipelines......Page 347
Encoding and decoding text......Page 348
Encoding strings as byte arrays......Page 349
Encoding and decoding text in files......Page 351
Serializing as XML......Page 352
Generating compact XML......Page 355
Deserializing XML files......Page 356
Serializing with JSON......Page 357
High-performance JSON processing......Page 358
Exercise 9.1 – Test your knowledge......Page 360
Exercise 9.3 – Explore topics......Page 361
Summary......Page 362
Understanding the vocabulary of protection......Page 364
Keys and key sizes......Page 365
Salts......Page 366
Encrypting and decrypting data......Page 367
Encrypting symmetrically with AES......Page 368
Hashing data......Page 372
Hashing with the commonly used SHA256......Page 373
Signing with SHA256 and RSA......Page 377
Generating random numbers for games......Page 381
Generating random numbers for cryptography......Page 382
What's new in cryptography......Page 383
Authenticating and authorizing users......Page 384
Implementing authentication and authorization......Page 386
Protecting application functionality......Page 389
Exercise 10.1 – Test your knowledge......Page 390
Exercise 10.4 – Explore topics......Page 391
Summary......Page 392
Understanding modern databases......Page 394
Understanding Entity Framework......Page 395
Using a sample relational database......Page 396
Creating the Northwind sample database for SQLite......Page 397
Managing the Northwind sample database with SQLiteStudio......Page 398
Choosing an EF Core data provider......Page 399
Connecting to the database......Page 400
EF Core conventions......Page 401
EF Core Fluent API......Page 402
Building an EF Core model......Page 403
Defining the Category and Product entity classes......Page 404
Defining the Northwind database context class......Page 406
Querying EF Core models......Page 407
Filtering and sorting products......Page 409
Logging EF Core......Page 410
Pattern matching with Like......Page 415
Loading patterns with EF Core......Page 417
Eager loading entities......Page 418
Enabling lazy loading......Page 419
Explicit loading entities......Page 420
Inserting entities......Page 422
Updating entities......Page 424
Deleting entities......Page 425
Transactions......Page 426
Defining an explicit transaction......Page 427
Exercise 11.1 – Test your knowledge......Page 428
Summary......Page 429
Writing LINQ queries......Page 430
Extending sequences with the Enumerable class......Page 431
Filtering entities with Where......Page 432
Targeting a named method......Page 434
Targeting a lambda expression......Page 435
Sorting by a single property using OrderBy......Page 436
Filtering by type......Page 437
Working with sets and bags using LINQ......Page 439
Using LINQ with EF Core......Page 441
Building an EF Core model......Page 442
Filtering and sorting sequences......Page 444
Projecting sequences into new types......Page 446
Joining and grouping sequences......Page 447
Aggregating sequences......Page 450
Sweetening LINQ syntax with syntactic sugar......Page 451
Creating an app that benefits from multiple threads......Page 453
For all operating systems......Page 454
Creating your own LINQ extension methods......Page 456
Generating XML using LINQ to XML......Page 460
Reading XML using LINQ to XML......Page 461
Exercise 12.2 – Practice querying with LINQ......Page 462
Summary......Page 463
Understanding processes, threads, and tasks......Page 464
Evaluating the efficiency of types......Page 466
Monitoring performance and memory use......Page 467
Implementing the Recorder class......Page 468
Measuring the efficiency of processing strings......Page 471
Running tasks asynchronously......Page 472
Running multiple actions synchronously......Page 473
Running multiple actions asynchronously using tasks......Page 474
Waiting for tasks......Page 476
Continuing with another task......Page 477
Nested and child tasks......Page 479
Synchronizing access to shared resources......Page 480
Accessing a resource from multiple threads......Page 481
Applying a mutually exclusive lock to a resource......Page 482
Understanding the lock statement and avoiding deadlocks......Page 483
Making CPU operations atomic......Page 485
Improving responsiveness for console apps......Page 486
Improving responsiveness for GUI apps......Page 487
Common types that support multitasking......Page 488
Working with async streams......Page 489
Exercise 13.1 – Test your knowledge......Page 490
Summary......Page 491
Understanding app models for C# and .NET......Page 492
Building websites using a web content management system......Page 493
Understanding web applications......Page 494
ASP.NET Core 1.0......Page 495
ASP.NET Core 2.1......Page 496
ASP.NET Core 3.0......Page 497
Understanding SignalR......Page 498
Understanding Blazor......Page 499
WebAssembly – a target for Blazor......Page 500
Understanding the bonus chapters......Page 501
Building cross-platform mobile apps......Page 502
Creating a class library for Northwind entity models......Page 503
Creating a class library for a Northwind database context......Page 508
Summary......Page 511
Understanding HTTP......Page 512
Client-side web development......Page 516
Understanding ASP.NET Core......Page 517
Classic ASP.NET versus modern ASP.NET Core......Page 518
Creating an ASP.NET Core project......Page 519
Testing and securing the website......Page 521
Enabling static and default files......Page 524
Enabling Razor Pages......Page 527
Defining a Razor Page......Page 528
Using shared layouts with Razor Pages......Page 529
Using code-behind files with Razor Pages......Page 531
Using Entity Framework Core with
ASP.NET Core......Page 533
Configure Entity Framework Core as a service......Page 534
Enabling a model to insert entities......Page 535
Defining a form to insert new suppliers......Page 536
Using Razor class libraries......Page 538
Using a Razor class library......Page 540
Exercise 15.1 – Test your knowledge......Page 541
Exercise 15.3 – Explore topics......Page 542
Summary......Page 543
Setting up an ASP.NET Core MVC website......Page 544
Creating and exploring an ASP.NET Core MVC website......Page 545
Reviewing the ASP.NET Core MVC website......Page 547
Reviewing the ASP.NET Core Identity database......Page 549
Understanding ASP.NET Core MVC startup......Page 550
Understanding the default MVC route......Page 552
Understanding controllers and actions......Page 553
Understanding filters......Page 555
Using a filter to cache a response......Page 556
Understanding entity and view models......Page 557
Understanding views......Page 559
Customizing an ASP.NET Core MVC website......Page 562
Understanding Razor syntax......Page 563
Defining a typed view......Page 564
Testing the customized home page......Page 567
Passing parameters using a route value......Page 568
Understanding model binders......Page 570
Validating the model......Page 574
Understanding view helper methods......Page 577
Querying a database and using display templates......Page 578
Improving scalability using asynchronous tasks......Page 580
Making controller action methods asynchronous......Page 581
Using other project templates......Page 582
Installing additional template packs......Page 583
Exercise 16.2 – Practice implementing MVC by implementing a category detail page......Page 584
Summary......Page 585
Understanding the benefits of a CMS......Page 588
Understanding enterprise CMS features......Page 589
Understanding Piranha CMS......Page 590
Creating and exploring a Piranha CMS website......Page 591
Editing site and page content......Page 594
Creating a new top-level page......Page 598
Creating a new child page......Page 599
Reviewing the blog archive......Page 601
Exploring authentication and authorization......Page 602
Exploring configuration......Page 605
Testing the new content......Page 606
Understanding routing......Page 607
Understanding the application service......Page 609
Understanding content types......Page 610
Understanding standard fields......Page 611
Reviewing some content types......Page 612
Understanding standard blocks......Page 615
Reviewing component types and standard blocks......Page 616
Reviewing the standard page type......Page 618
Reviewing the blog archive page type......Page 620
Defining custom content and component types......Page 621
Creating custom regions......Page 622
Creating an entity data model......Page 623
Creating custom page types......Page 624
Creating custom view models......Page 625
Defining custom content templates for content types......Page 626
Configuring start up and importing from a database......Page 629
Uploading images and creating the catalog root......Page 633
Importing category and product content......Page 634
Managing catalog content......Page 636
Reviewing how Piranha stores content......Page 638
Exercise 17.2 – Practice defining a block type for rendering YouTube videos......Page 640
Summary......Page 641
Understanding web service acronyms......Page 642
Creating an ASP.NET Core Web API project......Page 643
Reviewing the web service's functionality......Page 646
Creating a web service for the Northwind database......Page 648
Creating data repositories for entities......Page 650
Implementing a Web API controller......Page 653
Configuring the customers repository and Web API controller......Page 655
Specifying problem details......Page 660
Testing GET requests using a browser......Page 661
Testing HTTP requests with REST Client extension......Page 662
Enabling Swagger......Page 666
Testing requests with Swagger UI......Page 667
Understanding HttpClient......Page 672
Configuring HTTP clients using HttpClientFactory......Page 673
Enabling Cross-Origin Resource Sharing......Page 676
Implementing Health Check API......Page 678
Implementing Open API analyzers and conventions......Page 679
Configuring endpoint routing......Page 680
Understanding gRPC......Page 683
Exercise 18.1 – Test your knowledge......Page 684
Summary......Page 685
Understanding machine learning......Page 686
Understanding the machine learning life cycle......Page 687
Understanding datasets for training and testing......Page 688
Understanding machine learning tasks......Page 689
Understanding Microsoft Azure Machine Learning......Page 690
Understanding Infer.NET......Page 691
Understanding model training concepts......Page 692
Making product recommendations......Page 694
Problem analysis......Page 695
Data gathering and processing......Page 696
Creating the NorthwindML website project......Page 697
Creating the data and view models......Page 698
Implementing the controller......Page 701
Training the recommendation models......Page 703
Implementing a shopping cart with recommendations......Page 705
Testing the product recommendations website......Page 710
Exercise 19.1 – Test your knowledge......Page 713
Exercise 19.3 – Explore topics......Page 714
Summary......Page 715
Chapter 20: Building Windows Desktop Apps......Page 716
Understanding legacy Windows application platforms......Page 717
Installing Microsoft Visual Studio 2019 for Windows......Page 718
Building a new Windows Forms application......Page 719
Reviewing a new Windows Forms application......Page 721
Migrating a legacy Windows Forms application......Page 722
Migrating a Windows Forms app......Page 723
Understanding the modern Windows platform......Page 724
Filling user interface elements with acrylic brushes......Page 725
Understanding XAML Standard......Page 726
Simplifying code using XAML......Page 727
Understanding markup extensions......Page 728
Creating a UWP project......Page 729
Exploring common controls and acrylic brushes......Page 733
Exploring Reveal......Page 734
Installing more controls......Page 737
Sharing resources......Page 739
Replacing a control template......Page 740
Binding to elements......Page 742
Creating an HTTP service to bind to......Page 744
Downloading the web service's certificate......Page 747
Binding to data from a secure HTTP service......Page 748
Creating the user interface to call the HTTP service......Page 750
Converting numbers to images......Page 752
Testing the HTTP service data binding......Page 759
Practicing and exploring......Page 760
Exercise 20.2 – Explore topics......Page 761
Summary......Page 762
Chapter 21: Building Cross-Platform Mobile Apps Using
Xamarin.Forms......Page 764
How Xamarin.Forms extends Xamarin......Page 765
Mobile first, cloud first......Page 766
Understanding the INotificationPropertyChanged interface......Page 767
Understanding Xamarin.Forms user interface components......Page 768
Understanding the Entry and Editor controls......Page 769
Adding Android SDKs......Page 770
Creating a Xamarin.Forms solution......Page 771
Creating an entity model with two-way data binding......Page 773
Creating a component for dialing phone numbers......Page 777
Creating views for the customers list and customer details......Page 779
Implementing the customer list view......Page 780
Implementing the customer detail view......Page 783
Setting the main page for the mobile app......Page 785
Testing the mobile app......Page 786
Configuring the web service to allow insecure requests......Page 788
Configuring the iOS app to allow insecure connections......Page 789
Adding NuGet packages for consuming a web service......Page 790
Getting customers from the web service......Page 791
Exercise 21.1 – Test your knowledge......Page 792
Summary......Page 793
Epilogue......Page 794
Other Books You
May Enjoy......Page 796
Index......Page 800

Polecaj historie