Professional C# and .NET, 2021 Edition - Christian Nagel - E-Book

Professional C# and .NET, 2021 Edition E-Book

Christian Nagel

0,0
39,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

Get the latest coverage of the newest features in C#9 and .NET 5 In Professional C# and .NET: 2021 Edition, Microsoft MVP for Visual Studio and Development Technologies and veteran developer, Christian Nagel, delivers a comprehensive tour of the new features and capabilities of C#9 and .NET 5. Experienced programmers making the transition to C# will benefit from the author's in-depth explorations to create Web- and Windows applications using ASP.NET Core, Blazor, and WinUI using modern application patterns and new features offered by .NET including Microservices deployed to Docker images, GRPC, localization, asynchronous streaming, and much more. The book also offers: * Discussions of the extension of .NET to non-Microsoft platforms like OSX and Linux * Explanations of the newest features in C#9, including support for record types, and enhanced support for tuples, pattern matching, and nullable reference types * Integrating .NET applications with Microsoft Azure services such as Azure App * Configuration, Azure Key Vault, Azure Functions, the Azure Active Directory, and others Downloadable code examples from wrox.com and github.com with online updates for C# 10 and .NET 6 Perfect for programmers with a background in C#, Visual Basic, Java, or C/C++, Professional C# and .NET: 2021 Edition will also earn a place in the libraries of software architects seeking an up-to-date and fulsome treatment of the latest C# and .NET releases.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 1706

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

COVER

TITLE PAGE

INTRODUCTION

THE WORLD OF .NET

THE WORLD OF C#

WHAT'S NEW IN C#

WHAT'S NEW IN ASP.NET CORE

WHAT'S NEW WITH WINDOWS

WHAT YOU NEED TO WRITE AND RUN C# CODE

WHAT THIS BOOK COVERS

CONVENTIONS

SOURCE CODE

ERRATA

PART I: The C# Language

1 .NET Applications and Tools

FROM .NET FRAMEWORK TO .NET CORE TO .NET

.NET TERMS

.NET SUPPORT LENGTH

APPLICATION TYPES AND TECHNOLOGIES

DEVELOPER TOOLS

USING THE . NET CLI

SUMMARY

2 Core C#

FUNDAMENTALS OF C#

NULLABLE TYPES

USING PREDEFINED TYPES

CONTROLLING PROGRAM FLOW

ORGANIZATION WITH NAMESPACES

WORKING WITH STRINGS

COMMENTS

C# PREPROCESSOR DIRECTIVES

C# PROGRAMMING GUIDELINES

SUMMARY

3 Classes, Records, Structs, and Tuples

CREATING AND USING TYPES

PASS BY VALUE OR BY REFERENCE

CLASSES

RECORDS

STRUCTS

ENUM TYPES

REF, IN, AND OUT

TUPLES

VALUETUPLE

DECONSTRUCTION

PATTERN MATCHING

PARTIAL TYPES

SUMMARY

4 Object-Oriented Programming in C#

OBJECT ORIENTATION

INHERITANCE WITH CLASSES

MODIFIERS

INHERITANCE WITH RECORDS

USING INTERFACES

GENERICS

SUMMARY

5 Operators and Casts

OPERATORS

USING BINARY OPERATORS

TYPE SAFETY

OPERATOR OVERLOADING

COMPARING OBJECTS FOR EQUALITY

IMPLEMENTING CUSTOM INDEXERS

USER-DEFINED CONVERSIONS

SUMMARY

6 Arrays

MULTIPLE OBJECTS OF THE SAME TYPE

SIMPLE ARRAYS

MULTIDIMENSIONAL ARRAYS

JAGGED ARRAYS

ARRAY CLASS

ARRAYS AS PARAMETERS

ENUMERATORS

USING SPAN WITH ARRAYS

INDICES AND RANGES

ARRAY POOLS

BITARRAY

SUMMARY

7 Delegates, Lambdas, and Events

REFERENCING METHODS

DELEGATES

LAMBDA EXPRESSIONS

EVENTS

SUMMARY

8 Collections

OVERVIEW

COLLECTION INTERFACES AND TYPES

LISTS

STACKS

LINKED LISTS

SORTED LIST

DICTIONARIES

SETS

PERFORMANCE

IMMUTABLE COLLECTIONS

SUMMARY

9 Language Integrated Query

LINQ OVERVIEW

STANDARD QUERY OPERATORS

PARALLEL LINQ

EXPRESSION TREES

LINQ PROVIDERS

SUMMARY

10 Errors and Exceptions

HANDLING ERRORS

PREDEFINED EXCEPTION CLASSES

CATCHING EXCEPTIONS

USER-DEFINED EXCEPTION CLASSES

CALLER INFORMATION

SUMMARY

11 Tasks and Asynchronous Programming

WHY ASYNCHRONOUS PROGRAMMING IS IMPORTANT

TASK-BASED ASYNC PATTERN

TASKS

ERROR HANDLING

CANCELLATION OF ASYNC METHODS

ASYNC STREAMS

ASYNC WITH WINDOWS APPS

SUMMARY

12 Reflection, Metadata, and Source Generators

INSPECTING CODE AT RUNTIME AND DYNAMIC PROGRAMMING

CUSTOM ATTRIBUTES

USING REFLECTION

USING DYNAMIC LANGUAGE EXTENSIONS FOR REFLECTION

EXPANDOOBJECT

SOURCE GENERATORS

SUMMARY

13 Managed and Unmanaged Memory

MEMORY

MEMORY MANAGEMENT UNDER THE HOOD

STRONG AND WEAK REFERENCES

WORKING WITH UNMANAGED RESOURCES

UNSAFE CODE

SPAN<T>

PLATFORM INVOKE

SUMMARY

PART II: Libraries

14 Libraries, Assemblies, Packages, and NuGet

THE HELL OF LIBRARIES

ASSEMBLIES

CREATING AND USING LIBRARIES

CREATING NUGET PACKAGES

MODULE INITIALIZERS

SUMMARY

15 Dependency Injection and Configuration

WHAT IS DEPENDENCY INJECTION?

USING THE .NET DI CONTAINER

USING THE HOST CLASS

LIFETIME OF SERVICES

INITIALIZATION OF SERVICES USING OPTIONS

USING CONFIGURATION FILES

CONFIGURATION WITH .NET APPLICATIONS

AZURE APP CONFIGURATION

SUMMARY

16 Diagnostics and Metrics

DIAGNOSTICS OVERVIEW

LOGGING

METRICS

ANALYTICS WITH VISUAL STUDIO APP CENTER

APPLICATION INSIGHTS

SUMMARY

17 Parallel Programming

OVERVIEW

PARALLEL CLASS

TASKS

CANCELLATION FRAMEWORK

CHANNELS

TIMERS

THREADING ISSUES

INTERLOCKED

MONITOR

SPINLOCK

WAITHANDLE

MUTEX

SEMAPHORE

EVENTS

BARRIER

READERWRITERLOCKSLIM

LOCKS WITH AWAIT

SUMMARY

18 Files and Streams

OVERVIEW

MANAGING THE FILE SYSTEM

ITERATING FILES

WORKING WITH STREAMS

USING READERS AND WRITERS

COMPRESSING FILES

WATCHING FILE CHANGES

JSON SERIALIZATION

USING FILES AND STREAMS WITH THE WINDOWS RUNTIME

SUMMARY

19 Networking

OVERVIEW

WORKING WITH UTILITY CLASSES

USING SOCKETS

USING TCP CLASSES

USING UDP

USING WEB SERVERS

THE HTTPCLIENT CLASS

HTTPCLIENT FACTORY

SUMMARY

20 Security

ELEMENTS OF SECURITY

VERIFYING USER INFORMATION

ENCRYPTING DATA

ENSURING WEB SECURITY

SUMMARY

21 Entity Framework Core

INTRODUCING EF CORE

CREATING A MODEL

SCAFFOLDING A MODEL FROM THE DATABASE

MIGRATIONS

WORKING WITH QUERIES

LOADING RELATED DATA

WORKING WITH RELATIONSHIPS

SAVING DATA

CONFLICT HANDLING

USING TRANSACTIONS

USING AZURE COSMOS DB

SUMMARY

22 Localization

GLOBAL MARKETS

NAMESPACE SYSTEM.GLOBALIZATION

RESOURCES

LOCALIZATION WITH ASP.NET CORE

LOCALIZATION WITH WINUI

SUMMARY

23 Tests

OVERVIEW

UNIT TESTING

USING A MOCKING LIBRARY

ASP.NET CORE INTEGRATION TESTING

SUMMARY

PART III: Web Applications and Services

24 ASP.NET Core

UNDERSTANDING WEB TECHNOLOGIES

CREATING AN ASP.NET CORE WEB PROJECT

ADDING CLIENT-SIDE CONTENT

CREATING CUSTOM MIDDLEWARE

ENDPOINT ROUTING

REQUEST AND RESPONSE

SESSION STATE

HEALTH CHECKS

DEPLOYMENT

SUMMARY

25 Services

UNDERSTANDING TODAY'S SERVICES

REST SERVICES WITH ASP.NET CORE

CREATING A .NET CLIENT

USING EF CORE WITH SERVICES

AUTHENTICATION AND AUTHORIZATION WITH AZURE AD B2C

IMPLEMENTING AND USING SERVICES WITH GRPC

USING AZURE FUNCTIONS

MORE AZURE SERVICES

SUMMARY

26 Razor Pages and MVC

SETTING UP SERVICES FOR RAZOR PAGES AND MVC

RAZOR PAGES

ASP.NET CORE MVC

SUMMARY

27 Blazor

BLAZOR SERVER AND BLAZOR WEBASSEMBLY

CREATING A BLAZOR SERVER WEB APPLICATION

BLAZOR WEBASSEMBLY

RAZOR COMPONENTS

SUMMARY

28 SignalR

OVERVIEW

CREATING A SIMPLE CHAT USING SIGNALR

GROUPING CONNECTIONS

STREAMING WITH SIGNALR

SUMMARY

PART IV: Apps

29 Windows Apps

INTRODUCING WINDOWS APPS

INTRODUCING XAML

WORKING WITH CONTROLS

WORKING WITH DATA BINDING

IMPLEMENTING NAVIGATION

IMPLEMENTING LAYOUT PANELS

SUMMARY

30 Patterns with XAML Apps

WHY MVVM?

DEFINING THE MVVM PATTERN

SAMPLE SOLUTION

MODELS

SERVICES

VIEW MODELS

VIEWS

MESSAGING USING EVENTS

SUMMARY

31 Styling Windows Apps

STYLING

SHAPES

GEOMETRY

TRANSFORMATION

BRUSHES

STYLES AND RESOURCES

TEMPLATES

ANIMATIONS

VISUAL STATE MANAGER

SUMMARY

INDEX

COPYRIGHT

DEDICATION

ABOUT THE AUTHOR

ABOUT THE TECHNICAL EDITOR

ACKNOWLEDGMENTS

END USER LICENSE AGREEMENT

List of Illustrations

Chapter 1

FIGURE 1-1

FIGURE 1-2

Chapter 5

FIGURE 5-1

Chapter 6

FIGURE 6-1

FIGURE 6-2

FIGURE 6-3

FIGURE 6-4

FIGURE 6-5

FIGURE 6-6

FIGURE 6-7

Chapter 8

FIGURE 8-1

FIGURE 8-2

FIGURE 8-3

FIGURE 8-4

Chapter 9

FIGURE 9-1

FIGURE 9-2

Chapter 13

FIGURE 13-1

FIGURE 13-2

FIGURE 13-3

FIGURE 13-4

FIGURE 13-5

FIGURE 13-6

Chapter 14

FIGURE 14-1

FIGURE 14-2

FIGURE 14-3

Chapter 16

FIGURE 16-1

FIGURE 16-2

FIGURE 16-3

FIGURE 16-4

FIGURE 16-5

Chapter 17

FIGURE 17-1

Chapter 18

FIGURE 18-1

Chapter 19

FIGURE 19-1

FIGURE 19-2

Chapter 20

FIGURE 20-1

Chapter 22

FIGURE 22-1

FIGURE 22-2

FIGURE 22-3

FIGURE 22-4

FIGURE 22-5

FIGURE 22-6

FIGURE 22-7

Chapter 23

FIGURE 23-1

Chapter 24

FIGURE 24-1

FIGURE 24-2

Chapter 25

FIGURE 25-1

FIGURE 25-2

FIGURE 25-3

FIGURE 25-4

FIGURE 25-5

FIGURE 25-6

Chapter 26

FIGURE 26-1

FIGURE 26-2

FIGURE 26-3

FIGURE 26-4

FIGURE 26-5

FIGURE 26-6

FIGURE 26-7

FIGURE 26-8

FIGURE 26-9

FIGURE 26-10

Chapter 27

FIGURE 27-1

FIGURE 27-2

FIGURE 27-3

FIGURE 27-4

FIGURE 27-5

Chapter 28

FIGURE 28-1

FIGURE 28-2

FIGURE 28-3

FIGURE 28-4

FIGURE 28-5

Chapter 29

FIGURE 29-1

FIGURE 29-2

FIGURE 29-3

FIGURE 29-4

FIGURE 29-5

FIGURE 29-6

FIGURE 29-7

FIGURE 29-8

FIGURE 29-9

FIGURE 29-10

FIGURE 29-11

FIGURE 29-12

FIGURE 29-13

FIGURE 29-14

FIGURE 29-15

FIGURE 29-16

FIGURE 29-17

FIGURE 29-18

FIGURE 29-19

FIGURE 29-20

Chapter 30

FIGURE 30-1

FIGURE 30-2

FIGURE 30-3

FIGURE 30-4

Chapter 31

FIGURE 31-1

FIGURE 31-2

FIGURE 31-3

FIGURE 31-4

FIGURE 31-5

FIGURE 31-6

FIGURE 31-7

FIGURE 31-8

FIGURE 31-9

FIGURE 31-10

FIGURE 31-11

FIGURE 31-12

FIGURE 31-13

FIGURE 31-14

Guide

Cover

Title Page

Copyrigt

Dedication

About the Author

Acknowledgments

Introduction

Table of Contents

Begin Reading

Index

End User License Agreement

Pages

v

xxxix

xl

xli

xlii

xliii

xliv

xlv

xlvi

1

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

375

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

vi

vii

ix

xi

963

PROFESSIONALC# and .NET

 

2021 Edition

 

 

Christian Nagel

 

 

 

 

 

INTRODUCTION

EVEN THOUGH .NET was announced in the year 2000, it is not becoming a grandfather technology. Instead, .NET keeps increasing developer traction since it has become open source and is available not only on Windows but also on Linux platforms. .NET can also run within the browser on the client—without the need to install a plugin—by using the WebAssembly standard.

As new enhancements for C# and .NET are coming, a focus lies not only on performance gains but also on ease of use. .NET more and more is a choice for new developers.

C# is also attractive for long-term developers. Every year, Stack Overflow asks developers about the most loved, dreaded, and wanted programming languages and frameworks. For several years, C# has been within the top 10 of the most loved programming languages. ASP.NET Core now holds the top position as the most loved web framework. .NET Core is number one in the most loved other frameworks/libraries/tools category. See https://insights.stackoverflow.com/survey/2020 for details.

When you use C# and ASP.NET Core, you can create web applications and services (including microservices) that run on Windows, Linux, and Mac. You can use the Windows Runtime to create native Windows apps using C#, XAML, and .NET. You can create libraries that you share between ASP.NET Core, Windows apps, and .NET MAUI. You can also create traditional Windows Forms and WPF applications.

Most of the samples of this book are built to run on a Windows or Linux system. Exceptions are the Windows app samples that run only on the Windows platform. You can use Visual Studio, Visual Studio Code, or Visual Studio for the Mac as the developer environment; only the Windows app samples require Visual Studio.

THE WORLD OF .NET

.NET has a long history; the first version was released in the year 2002. The new .NET generation with a complete rewrite of .NET (.NET Core 1.0 in the year 2016) is very young. Recently, many features from the old .NET version have been brought to .NET Core to ease the migration experience.

When creating new applications, there is no reason not to move to the new .NET versions. Whether old applications should stay with the old version of .NET or be migrated to the new one depends on the features used, how difficult the migration is, and what advantages you gain after the application is migrated. The best options here need to be considered with an application-by-application analysis.

The new .NET provides easy ways to create Windows and web applications and services. You can create microservices running in Docker containers in a Kubernetes cluster; create web applications; use the new OpenTelemetry standard to analyze distributed traces in a vendor-independent manner; create web applications returning HTML, JavaScript, and CSS; and create web applications returning HTML, JavaScript, and .NET binaries that run in the client's browser in a safe and standard way using WebAssembly. You can create Windows applications in traditional ways using WPF and Windows Forms and make use of modern XAML features and controls that support the fluent design with WinUI and mobile applications with .NET MAUI.

.NET uses modern patterns. Dependency injection is built into core services, such as ASP.NET Core and EF Core, which not only makes unit testing easier but also allows developers to easily enhance and change features from these technologies.

.NET runs on multiple platforms. Besides Windows and macOS, many Linux environments are supported, such as Alpine, CentOS, Debian, Fedora, openSUSE, Red Hat, SLES, and Ubuntu.

.NET is open source (https://github.com/dotnet) and freely available. You can find meeting notes for the C# compiler (https://github.com/dotnet/csharplang), the source code for the C# compiler (https://github.com/dotnet/Roslyn), the .NET runtime and libraries (https://github.com/dotnet/runtime), and ASP.NET Core (https://github.com/dotnet/aspnetcore) with Razor Pages, Blazor, and SignalR.

Here's a summary of some of the features of the new .NET:

.NET is open source.

.NET uses modern patterns.

.NET supports development on multiple platforms.

ASP.NET Core can run on Windows and Linux.

THE WORLD OF C#

When C# was released in the year 2002, it was a language developed for the .NET Framework. C# was designed with ideas from C++, Java, and Pascal. Anders Hejlsberg had come to Microsoft from Borland and brought experience from the language development of Delphi. At Microsoft, Hejlsberg worked on Microsoft's version of Java, named J++, before creating C#.

NOTE Today, Anders Hejlsberg has moved to TypeScript (although he still influences C#), and Mads Torgersen is the project lead for C#. C# improvements are discussed openly at https://github.com/dotnet/csharplang, and you can read C# language proposals and event meeting notes. You can also submit your own proposals for C#.

C# started not only as an object-oriented general-purpose programming language but was a component-based programming language that supported properties, events, attributes (annotations), and building assemblies (binaries including metadata).

Over time, C# was enhanced with generics, Language Integrated Query (LINQ), lambda expressions, dynamic features, and easier asynchronous programming. C# is not an easy programming language because of the many features it offers, but it's continuously evolving with features that are practical to use. With this, C# is more than an object-oriented or component-based language; it also includes ideas of functional programming—things that are of practical use for a general-purpose language developing all kinds of applications.

Nowadays, a new version of C# is released every year. C# 8 added nullable reference types, and C# 9 added records and more. C# 10 is releasing with .NET 6 in 2021 and C# 11 will be released with .NET 7 in 2022. Because of the frequency of changes nowadays, check the GitHub repository for the book (read more in the section “Source Code”) for continuous updates.

WHAT'S NEW IN C#

Every year, a new version of C# is released, with many new features available in each version. The latest versions include features such as nullable reference types to reduce exceptions of type NullableReferenceException and instead let the compiler help more; features to increase productivity such as indices and ranges; switch expressions that make the switch statement look old; features for using declarations; and enhancements with pattern matching. Top-level statements allow reducing the number of source code lines with small applications and records—classes where the compiler creates boilerplate code for equality comparison, deconstruction, and with expressions. Code generators allow creating code automatically while the compiler runs. All these new features are covered in this book.

WHAT'S NEW IN ASP.NET CORE

ASP.NET Core now contains new technology for creating web applications: Blazor Server and Blazor WebAssembly. With Blazor, you have a full-stack option to write C# code both for the client and for the server. With Blazor Server, the Razor components you create containing HTML and C# code run on the server. With Blazor WebAssembly, Razor components written with C# and HTML run on the client using the HTML 5 standard WebAssembly that allows you to run binary code in the browser, which is supported by all modern web browsers.

For creating services, you can now use gRPC with ASP.NET Core for binary communication between services. This is a great option for service-to-service communication to reduce the bandwidth needed, as well as CPU and memory usage if a lot of data transfer is needed.

WHAT'S NEW WITH WINDOWS

For developing applications for Windows, a new technology combines the features of the Universal Windows Platform and desktop applications: WinUI 3. WinUI is the native UI platform for Windows 10 applications. With WinUI 3, you can use modern XAML code that includes compiled binding to create desktop applications. New controls with Microsoft's fluent design system are available. These controls are not delivered with the Windows Runtime as was previously the case with the Universal Windows Platform (UWP). These controls are developed independently of the Windows 10 version that allows you to use the newest controls with Windows 10 versions 1809 and above. As the roadmap available with WinUI shows, these new controls will be usable from WPF applications as well.

WHAT YOU NEED TO WRITE AND RUN C# CODE

.NET runs on Windows, Linux, and Mac operating systems. You can create and build your programs on any of these operating systems using Visual Studio Code (https://code.visualstudio.com). You can build and run most of the samples on Windows or Linux and use the .NET development tools of your choice. Only the WinUI applications require you to use the Windows platform, and here, Visual Studio is the best option to use. The minimum version required to build and run the WinUI application is version 16.10.

The command line plays an important part when using the .NET CLI and the Azure CLI; you can use the new Windows Terminal. With the newest Windows 10 versions, this terminal is delivered as part of Windows. With older versions, you can download it from the Microsoft Store.

Most .NET developers use the Windows platform as their development machine. When using the Windows Subsystem for Linux (WSL 2), you can build and run your .NET applications in a Linux environment, and you can install different Linux distributions from your Windows environment and access the same files. Visual Studio even allows debugging your .NET applications while they run in a Linux environment on WSL 2.

With some samples of the book, Microsoft Azure is shown as an optional hosting environment to run your web applications, use Azure Functions, and use Entity Framework Core to access SQL Server and Azure Cosmos DB. For this, you can use a free trial offering from Microsoft Azure; visit https://azure.microsoft.com/free to register.

WHAT THIS BOOK COVERS

This book covers these four major parts:

The C# language

Using base class libraries from .NET

Developing web applications and services

Developing Windows applications

Let's get into the different parts and all the chapters in more detail.

Part I, “The C# Language”

The first part of this book covers all the aspects of the C# programming language. You learn the syntax options and see how the C# syntax integrates with classes and interfaces from .NET. This part gives good grounding in the C# language. This section doesn't presume knowledge of any particular programming language, but it's assumed you are an experienced programmer. You start looking at C#'s basic syntax and data types before getting into advanced C# features.

Chapter 1

, “.NET Applications and Tools,” covers what you need to know to create .NET applications. You learn about the .NET CLI and create a Hello World application using C# 9 top-level statements.

Chapter 2

, “Core C#,” dives into core C# features and gives you details on top-level statements and information on declaration of variables and data types. The chapter covers target-typed new expressions, explains nullable reference types, and defines a program flow that includes the new

switch

expressions.

Chapter 3

, “Classes, Records, Structs, and Tuples,” gives you information to create reference or value types, create and use tuples, and make use of the C# 9 enhancement to create and use records.

Chapter 4

, “Object-Oriented Programming in C#,” goes into details of object-oriented techniques with C# and demonstrates all the C# keywords for object orientation. It also covers using inheritance with C# 9 records.

Chapter 5

, “Operators and Casts,” explains the C# operators, and you also learn how to overload standard operators for custom types.

Chapter 6

, “Arrays,” doesn't stop with simple arrays; you learn using multidimensional and jagged arrays, use the

Span

type to access arrays, and use the new index and range operators to access arrays.

Chapter 7

, “Delegates, Lambdas, and Events,” covers .NET pointers to methods, lambda expressions with closures, and .NET events.

Chapter 8

, “Collections,” dives into the different kind of collections, such as lists, queues, stacks, dictionaries, and immutable collections. The chapter also gives you the information you need to decide which collection to use in what scenario.

Chapter 9

, “Language Integrated Query,” gives you the C# language integrated query features to query data from your collections. You also learn how to use multiple CPU cores with a query and what's behind expression trees that are used when you use LINQ to access your database with Entity Framework Core.

Chapter 10

, “Errors and Exceptions,” covers how you should deal with errors, throw and catch exceptions, and filter exceptions when catching them.

Chapter 11

, “Tasks and Asynchronous Programming,” shows the C# keywords

async

and

await

in action— not only with the task-based async pattern but also with async streams, which is a new feature since C# 8.

Chapter 12

, “Reflection, Metadata, and Source Generators,” covers using and reading attributes with C#. The attributes will not just be read using reflection, but you'll also see the functionality of source generators that allow creating source code during compile time.

Chapter 13

, “Managed and Unmanaged Memory,” is the last chapter of

Part I

, which not only shows using the

IDisposable

interface with the

using

statement and the new

using

declaration but also demonstrates using the

Span

type with managed and unmanaged memory. You can read about using Platform Invoke both with Windows and with Linux environments.

Part II, “Libraries”

Part II starts with creating custom libraries and NuGet packages, but the major topics covered with Part II are for using .NET libraries that are important for all application types.

Chapter 14

, “Libraries, Assemblies, Packages, and NuGet,” explains the differences between assemblies and NuGet packages. In this chapter, you learn how to create NuGet packages and are introduced to a new C# feature, module initializers, which allow you to run initial code in a library.

Chapter 15

, “Dependency Injection and Configuration,” gives detail about how the

Host

class is used to configure a dependency injection container and the built-in options to retrieve configuration information from a .NET application with different configuration providers, including Azure App Configuration and user secrets.

Chapter 16

, “Diagnostics and Metrics,” continues using the

Host

class to configure logging options. You also learn about reading metric information that's offered from some NET providers, using Visual Studio App Center, and extending logging for distributed tracing with OpenTelemetry.

Chapter 17

, “Parallel Programming,” covers myriad features available with .NET for parallelization and synchronization.

Chapter 11

shows the core functionality of the

Task

class. In

Chapter 17

, more of the

Task

class is shown, such as forming task hierarchies and using value tasks. The chapter goes into issues of parallel programming such as race conditions and deadlocks, and for synchronization, you learn about different features available with the

lock

keyword, the

Monitor

,

SpinLock

,

Mutex

,

Semaphore

classes, and more.

Chapter 18

, “Files and Streams,” not only covers reading and writing from the file system with new stream APIs that allow using the

Span

type but also covers the new .NET JSON serializer with classes in the

System.Text.Json

namespace.

In

Chapter 19

, “Networking,” you learn about foundational classes for network programming, such as the

Socket

class and how to create applications using TCP and UDP. You also use the

HttpClient

factory pattern to create

HttpClient

objects with automatic retries if transient errors occur.

Chapter 20

, “Security,” gives you information about cryptography classes for encrypting data, explains how to use the new

Microsoft.Identity

platform for user authentication, and provides information on web security and what you need to be aware of with encoding issues as well as cross-site request forgery attacks.

Chapter 21

, “Entity Framework Core,” covers reading and writing data from a database—including the many features offered from EF Core, such as shadow properties, global query filters, many-to-many relations, and what metric information is now offered by EF Core—and reading and writing to Azure Cosmos DB with EF Core.

In

Chapter 22

, “Localization,” you learn to localize applications using techniques that are important both for Windows and web applications.

Chapter 23

, “Tests,” covers creating unit tests, analyzing code coverage with the .NET CLI, using a mocking library when creating unit tests, and what features are offered by ASP.NET Core to create integration tests.

Part III, “Web Applications and Services”

Part III of this book is dedicated to ASP.NET Core technologies for creating web applications and services, no matter whether you run these applications and services in your on-premises environment or in the cloud making use of Azure App Services, Azure Static Web Apps, or Azure Functions.

Chapter 24

, “ASP.NET Core,” gives you the foundation of ASP.NET Core. Based on the dependency injection container you learned about in

Part II

, this chapter shows how ASP.NET Core makes use of middleware to add functionality to every HTTP request and define routes with ASP.NET Core endpoint routing.

Chapter 25

, “Services,” dives into creating microservices using different technologies such as ASP.NET Core as well as using Azure Functions and gRPC for binary communication.

Chapter 26

, “Razor Pages and MVC,” is about interacting with users with ASP.NET Core technologies. It covers Razor pages, Razor views, and functionality such as tag helpers and view components.

Chapter 27

, “Blazor,” is about the newest enhancement of ASP.NET Core with Razor components, which allows you to implement C# code running either on the server or in the client using WebAssembly. You learn about the differences between Blazor Server and Blazor WebAssembly, what the restrictions are with these technologies, and the built-in components available.

Chapter 28

, “SignalR,” covers the real-time functionality available with ASP.NET Core to send information to a group of clients and how you can use C# async streams with SignalR.

Part IV, “Apps”

Part IV of this book is dedicated to XAML code and creating Windows applications with the native UI platform for Windows 10: WinUI. Much of the information you get here can also be applied to WPF applications and to .NET MAUI and developing XAML-based applications for mobile platforms.

Chapter 29

, “Windows Apps,” gives you foundational information on XAML, including dependency properties and attached properties. You learn how to create custom markup extensions and about the control categories available with WinUI, including advanced techniques such as adaptive triggers and deferred loading.

Chapter 30

, “Patterns with XAML Apps,” gives you the information you need to use the MVVM pattern and how you can share as much code as possible between different XAML-based technologies such as WinUI, WPF, and .NET MAUI.

Chapter 31

, “Styling Windows Apps,” explains XAML shapes and geometry elements, dives into styles and control templates, gives you information on creating animations, and explains how you can use the Visual State Manager with your XAML-based applications.

CONVENTIONS

To help you get the most from the text and keep track of what's happening, I use some conventions throughout the book.

WARNING Warnings hold important, not-to-be-forgotten information that is directly relevant to the surrounding text.

NOTE Notes indicate notes, tips, hints, tricks, and/or asides to the current discussion.

As for styles in the text:

We

highlight

new terms and important words when we introduce them.

We show keyboard strokes like this: Ctrl+A.

We show filenames, URLs, and code within the text like so:

persistence.properties

.

We present code in two different ways:

We use a monofont type with no highlighting for most code examples.

We use bold to emphasize code that's particularly important in the present context or to show changes from a previous code snippet.

SOURCE CODE

As you work through the examples in this book, you may choose either to type all the code manually or to use the source code files that accompany the book. All the source code used in this book is available for download at www.wiley.com. When at the site, simply locate the book's title (either by using the Search box or by using one of the title lists) and click the Download Code link on the book's detail page to obtain all the source code for the book.

NOTE Because many books have similar titles, you may find it easiest to search by ISBN; this book's ISBN is 978-1-119-79720-3.

After you download the code, just decompress it with your favorite compression tool.

The source code is also available on GitHub at https://www.github.com/ProfessionalCSharp/ProfessionalCSharp2021. With GitHub, you can also open each source code file with a web browser. When you use the website, you can download the complete source code in a zip file. You can also clone the source code to a local directory on your system. Just install the Git tools, which you can do with Visual Studio or by downloading the Git tools from https://git-scm.com/downloads for Windows, Linux, and Mac. To clone the source code to a local directory, use git clone:

> git clone https://www.github.com/ProfessionalCSharp/ProfessionalCSharp2021

With this command, the complete source code is copied to the subdirectory ProfessionalCSharp2021. From there, you can start working with the source files.

As updates of .NET become available (until the next edition of the book will be released), the source code will be updated on GitHub. Check the readme.md file in the GitHub repo for updates. If the source code changes after you cloned it, you can pull the latest changes after changing your current directory to the directory of the source code:

> git pull

In case you've made some changes on the source code, git pull might result in an error. If this happens, you can stash away your changes and pull again:

> git stash

> git pull

The complete list of git commands is available at https://git-scm.com/docs.

In case you have questions on the source code, use discussions with the GitHub repository. If you find an error with the source code, create an issue. Open https://github.com/ProfessionalCSharp/ProfessionalCSharp2021 in the browser, click the Issues tab, and click the New Issue button. This opens an editor. Just be as descriptive as possible to describe your issue.

For reporting issues, you need a GitHub account. If you have a GitHub account, you can also fork the source code repository to your account. For more information on using GitHub, check https://guides.github.com/activities/hello-world.

NOTE You can read the source code and issues and clone the repository locally without joining GitHub. For posting issues and creating your own repositories on GitHub, you need your own GitHub account. For basic functionality, GitHub is free (see https://github.com/pricing).

ERRATA

We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be grateful for your feedback. By sending in errata, you may save another reader hours of frustration, and at the same time you can help provide even higher-quality information.

To find the errata page for this book, go to www.wiley.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page, you can view all errata that have been submitted for this book and posted by the book's editors.

If you don't spot “your” error on the Book Errata page, go to https://support.wiley.com/s/article/reporting-a-wiley-book-error for information about how to send us the error you have found. We'll check the information and, if appropriate, post a message to the book's errata page and fix the problem in subsequent editions of the book.

PART IThe C# Language

CHAPTER 1:

.NET Applications and Tools

CHAPTER 2:

Core C#

CHAPTER 3:

Classes, Records, Structs, and Tuples

CHAPTER 4:

Object-Oriented Programming in C#

CHAPTER 5:

Operators and Casts

CHAPTER 6:

Arrays

CHAPTER 7:

Delegates, Lambdas, and Events

CHAPTER 8:

Collections

CHAPTER 9:

Language Integrated Query

CHAPTER 10:

Errors and Exceptions

CHAPTER 11:

Tasks and Asynchronous Programming

CHAPTER 12:

Reflection, Metadata, and Source Generators

CHAPTER 13:

Managed and Unmanaged Memory

1.NET Applications and Tools

WHAT'S IN THIS CHAPTER?

From .NET Framework to .NET Core to .NET

.NET terms

.NET support length

Application types and technologies

Developer tools

Using the .NET command-line interface

Programming “Hello World!”

Technologies for creating web apps

CODE DOWNLOADS FOR THIS CHAPTER

The source code for this chapter is available on the book page at www.wiley.com. Click the Downloads link. The code can also be found at https://github.com/ProfessionalCSharp/ProfessionalCSharp2021 in the directory 1_CS/HelloWorld.

The code for this chapter is divided into the following major examples:

HelloWorld

WebApp

SelfContainedHelloWorld

FROM .NET FRAMEWORK TO .NET CORE TO .NET

The first version of .NET was released in 2002. Since the first version, many things have changed. The first era of .NET was the .NET Framework that offered Windows Forms for Windows desktop development and Web Forms to create web applications. This version of .NET was available only for Microsoft Windows. At that time, Microsoft also invented a standard for C# at ECMA (https://www.ecma-international.org/publications/standards/Ecma-334.htm).

Later, Silverlight used a subset of this technology with a limited library and runtime running in browsers using a browser add-in. At that time, the company Ximian developed the Mono runtime. This runtime was available for Linux and Android and offered a subset of Microsoft .NET’s functionality. Later, Novell bought Ximian, and Novell was later bought by The Attachmate Group. As the new organization lost interest in .NET, Miguel de Icaza (the founder of Ximian) started Xamarin and took the interesting .NET parts into his new organization to start .NET for Android and iOS. Nowadays, Xamarin belongs to Microsoft, and the Mono runtime is part of the dotnet runtime repo (https://github.com/dotnet/runtime).

Silverlight started .NET development for other devices with different form factors, which have different needs for .NET. Silverlight was not successful in the long term because HTML5 offered features that previously only were available by using browser add-ins. However, Silverlight started moving .NET in other directions that resulted in .NET Core.

.NET Core was the biggest change to .NET since its inception. .NET code became open-source, you could create apps for other platforms, and the new code base of .NET is using modern design patterns. The next step is a logical move: the version of .NET after .NET Core 3.1 is .NET 5. The Core name is removed, and version 4 was skipped to send a message to .NET Framework developers that there's a higher version than .NET Framework 4.8, and it's time to move to .NET 5 for creating new applications.

For developers using .NET Core, the move is an easy one. With existing applications, usually all that needs to be changed is the version number of the target framework. Moving applications from the .NET Framework is not that easy and might require bigger changes. Depending on the application type, more or less change is needed. .NET Core 3.x supports WPF and Windows Forms applications. With these application types, the change can be easy. However, existing .NET Framework WPF applications may have features that cannot be moved that easily to the new .NET. For example, application domains are not supported with .NET Core and .NET 5. Moving Windows Communication Foundation (WCF) services to .NET 5 is not at all easy. The server part of WCF is not supported in the new .NET era. The WCF part of the application needs to be rewritten to ASP.NET Core Web API, gRPC, or another communication technology that fulfills the needs.

With existing applications, it can be useful to stay with the .NET Framework instead of changing to the new .NET because the old framework will still be maintained for many years to come. The .NET Framework is installed with Windows 10, and support for the .NET Framework has a long target that is bound to the support of the Windows 10 versions.

The new .NET and NuGet packages allow Microsoft to provide faster update cycles for delivering new features. It's not easy to decide what technology should be used for creating applications. This chapter helps you with that decision. It gives you information about the different technologies available for creating Windows and web apps and services, offers guidance on what to choose for database access, and helps with moving from old technologies to new ones. You'll also read about the .NET tooling that you can use with the code samples through all the chapters of this book.

.NET TERMS

Before digging deeper, you should understand concepts and some important .NET terms, such as what's in the .NET SDK and what the .NET runtime is. You also should get a better understanding of the .NET Framework and .NET, when to use the .NET Standard, and the NuGet packages and .NET namespaces.

.NET SDK

For developing .NET applications, you need to install the .NET SDK. The SDK contains the .NET command-line interface (CLI), tools, libraries, and the runtime. With the .NET CLI, you can create new applications based on templates, restore packages, build and test the application, and create deployment packages. Later in this chapter in the section “.NET CLI,” you will see how to create and build applications.

If you use Visual Studio 2019, the .NET SDK is installed as part of Visual Studio. If you don't have Visual Studio, you can install the SDK from https://dot.net. Here, you can find instructions on how to install the SDK on Windows, Mac, and Linux systems.

You can install multiple versions of the .NET SDK in parallel. The command

> dotnet --list-sdks

shows all the different SDK versions that are installed on the system. By default, the latest version is used.

NOTE To run the command, you have many different options to start a command prompt. One is the Windows built-in Command Prompt; you can install the new Windows Terminal; if Visual Studio is installed, you can start the Developer Command Prompt; or you can use the bash shell. Read more on the Windows Terminal later in this chapter in the section “Developer Tools.”

You can create a global.json file if you do not want to use the latest version of the SDK. The command

> dotnet new globaljson

creates the file global.json in the current directory. This file contains the version element with the version number currently used. You can change the version number to one of the other SDK versions that is installed:

{

"sdk": {

"version": "5.0.202"

}

}

In the directory of global.json and its subdirectories, the specified SDK version is used. You can verify this with

> dotnet --version

.NET Runtime

On the target system, the .NET SDK is not required. Here you just need to install the .NET runtime. The runtime includes all the core libraries and the dotnet driver.

The dotnet driver is used to run the application—for example, the Hello, World application with

> dotnet hello-world.dll

At https://dot.net, you can find not only instructions to download and install the SDK on different platforms but also the runtime.

Instead of installing the runtime on the target system, you also can deliver the runtime as part of the application (which is known as self-contained deployment). This technique is very different from older .NET Framework applications and is covered later in the chapter in the “Using the .NET CLI” section.

To see which runtimes are installed, you can use

> dotnet --list-runtimes

Common Language Runtime

The C# compiler compiles C# code to Microsoft Intermediate Language (IL) code. This code is a little bit like assembly code, but it has more object-oriented features. The IL code is run by the Common Language Runtime (CLR). What's done by a CLR?

The IL code is compiled to native code by the CLR. The IL code available in .NET assemblies is compiled by a Just-In-Time (JIT) compiler. This compiler creates platform-specific native code. The runtime includes a JIT compiler named RyuJIT. This compiler is not only faster than the previous one, but it also has better support for using Edit & Continue while you're debugging the application with Visual Studio.

The runtime also includes a type system with a type loader that is responsible for loading types from assemblies. Security infrastructure with the type system verifies whether certain type system structures are permitted—for example, with inheritance.

After instances of types are created, they also need to be destroyed, and memory needs to be recycled. Another feature of the runtime is the garbage collector. The garbage collector cleans up memory from objects that are no longer referenced in the managed heap.

The runtime is also responsible for threading. When you are creating a managed thread from C#, it is not necessarily a thread from the underlying operating system. Threads are virtualized and managed by the runtime.

NOTE How you can create and manage threads from C# is covered in Chapter 17, “Parallel Programming.” Chapter 13, “Managed and Unmanaged Memory,” gives information about the garbage collector and how to clean up memory.

.NET Compiler Platform

The C# compiler that's installed as part of the SDK belongs to the .NET Compiler Platform, which is also known by the code name Roslyn. Roslyn allows you to interact with the compilation process, work with syntax trees, and access the semantic model that is defined by language rules. You can use Roslyn to write code analyzers and refactoring features. You also can use Roslyn with a new feature of C# 9, code generators, which are discussed in Chapter 12, “Reflection, Metadata, and Source Generators.”

.NET Framework

The .NET Framework is the name of the old .NET. The last version available is .NET Framework 4.8. It's not that useful to create new applications with this framework, but of course you can maintain existing applications because this technology will still be supported for many years to come. If existing applications don't get any advantages by moving to new technologies and there's not a lot of maintenance going on, there's no need to switch in the short term.

Depending on the technologies used with existing applications, the switch to .NET can be easy. WPF and Windows Forms have been offered with newer technologies since .NET Core 3. However, WPF and Windows applications could have used features where the application architecture might need a change.

Examples of technologies that are no longer offered with new versions of .NET are ASP.NET Web Forms, Windows Communication Foundation (WCF), and Windows Workflow Foundation (WF). Instead of ASP.NET Web Forms, you can rewrite applications using ASP.NET Blazor. Instead of WCF, you can use ASP.NET Core Web API or gRPC. Instead of WF, moving to Azure Logic Apps might be useful.

.NET Core

.NET Core is the new .NET that is used by all new technologies and is a main focus of this book (with the new name .NET). This framework is open source, and you can find it at http://www.github.com/dotnet. The runtime is the CoreCLR repository; the framework containing collection classes, file system access, console, XML, and a lot more is in the CoreFX repository.

Unlike the .NET Framework, where the specific version you needed for the application had to be installed on the system, with .NET Core 1.0, the framework, including the runtime, is delivered with the application. Previously, there were times when you might have had problems deploying an ASP.NET web application to a shared server because the provider had older versions of .NET installed; those times are gone. Now you can deliver the runtime with the application, and you are not dependent on the version installed on the server.

.NET Core is designed in a modular approach. The framework is split into a large list of NuGet packages. So that you don't have to deal with all the packages, metapackages reference the smaller packages that work together. This even improved with .NET Core 2.0 and ASP.NET Core 2.0. With ASP.NET Core 2.0, you just need to reference Microsoft.AspNetCore.All to get all the packages you typically need with ASP.NET Core web applications.

.NET Core can be updated at a fast pace. Even updating the runtime doesn't influence existing applications because the runtime can be installed with the applications. Now Microsoft can improve .NET Core, including the runtime, with faster release cycles.

NOTE For developing apps using .NET Core, Microsoft created new command-line utilities. These tools are introduced later in this chapter through a “Hello World!” application in the section “Using the .NET CLI.”

.NET

Starting with .NET 5, .NET Core has a new name: .NET. Removing “Core” from the name should tell developers who still use the .NET Framework that there's not a new version of the .NET Framework from now on. The .NET Framework is no longer receiving new features. For new applications, you should use .NET.

.NET Standard

.NET Standard is an important specification when creating and using libraries. .NET Standard offers a contract rather than an implementation. With this contract, available APIs are listed. With every new version of .NET Standard, new APIs are added. APIs are never removed. For example, .NET Standard 2.1 lists more APIs than .NET Standard 1.6.

When you're creating a library, you probably want to use as many APIs as possible, so I suggest you choose the most recent .NET Standard version. However, the highest standard version also means the lowest number of platforms that support this standard, so you may need to take that into consideration.

A table at https://docs.microsoft.com/dotnet/standard/net-standard gives you the details on what platform supports which version of the standard. For example, .NET Framework 4.6.1 and later support up to .NET Standard 2.0. In addition, .NET Core 3.0 and later (which includes .NET 5 and later) support .NET Standard 2.1. The Universal Windows Platform build 10.0.16299 supports .NET Standard 2.0. Xamarin.Android 10.0 supports .NET Standard 2.1.

As of .NET 5, the .NET Standard becomes irrelevant. If you're creating libraries with .NET 5, you can use libraries from .NET 5, .NET 6, and later applications. Similarly, when you're creating libraries with .NET 7, you can use libraries from applications written with .NET 7 and later.

However, we can't expect that the .NET Framework, Mono, and other older technologies will just fade away, so .NET Standard will still be needed for many years to come. If you need to support older technologies with your libraries, you'll still need .NET Standard.

NOTE Read detailed information about .NET Standard in Chapter 14, “Libraries, Assemblies, Packages, and NuGet.”

NuGet Packages

In the early days, assemblies were reusable units with applications. That use is still possible (and necessary with some assemblies) when you're adding a reference to an assembly for using the public types and methods from your own code. However, using libraries can mean a lot more than just adding a reference and using it. Using libraries can also mean making some configuration changes or using scripts to take advantage of some features. The target framework determines which binaries you can use. These are reasons to package assemblies within NuGet packages, which are zip files that contain the assembly (or multiple assemblies) as well as configuration information and PowerShell scripts.