What this book covers
Chapter 1, Hello, C#! Welcome, .NET!, is about setting up your development environment to use either Visual Studio or VS Code with C# Dev Kit. Then you will learn how to use them to create the simplest application possible with C# and .NET. For simplified console apps, you will see the use of the top-level program feature introduced in C# 9, which is then used by default in the project templates for C# 10 onwards. You will also learn about some good places to look for help, including AI tools like ChatGPT and GitHub Copilot, and ways to contact me to get help with an issue or give me feedback to improve the book today through its GitHub repository and in future print editions.
Chapter 2, Speaking C#, introduces the versions of C# and has tables showing which version introduced new features in an online section. I will explain the grammar and vocabulary that you will use every day to write the source code for your applications. In particular, you will learn how to declare and work with variables of different types.
Chapter 3, Controlling Flow, Converting Types, and Handling Exceptions, covers using operators to perform simple actions on variables, including comparisons, writing code that makes decisions, pattern matching, repeating a block of statements, and converting between types. This chapter also covers writing code defensively to handle exceptions when they inevitably occur, including using guard clauses like ThrowIfLessThan
on the ArgumentOutOfRangeException
class introduced with .NET 8.
Chapter 4, Writing, Debugging, and Testing Functions, is about following the Don’t Repeat Yourself (DRY) principle by writing reusable functions using both imperative and functional implementation styles. You will also learn how to use debugging tools to track down and remove bugs, use Hot Reload to make changes while your app is running, monitor your code while it executes to diagnose problems, and rigorously test your code to remove bugs, ensuring stability and reliability before it gets deployed into production.
Chapter 5, Building Your Own Types with Object-Oriented Programming, discusses all the different categories of members that a type like a class can have, including fields to store data and methods to perform actions. You will use Object-Oriented Programming (OOP) concepts, such as aggregation and encapsulation, and how to manage namespaces for types, including the ability to alias any type introduced with C# 12. You will learn language features such as tuple syntax support and out
variables, local functions, and default literals and inferred tuple names. You will also learn how to define and work with immutable types using the record
keyword, init
-only properties, and with
expressions, introduced in C# 9. Finally, we look at how C# 11 introduced the required
keyword to help avoid the overuse of constructors to control initialization, how C# 12 introduced primary constructors for non-record types, and how C# 13 expands the supported types for a params
parameter and how you can now define partial properties as well as partial methods.
Chapter 6, Implementing Interfaces and Inheriting Classes, explains deriving new types from existing ones using OOP. You will learn how to define operators, delegates, and events, how to implement interfaces about base and derived classes, how to override a member of a type, how to use polymorphism, how to create extension methods, how to cast between classes in an inheritance hierarchy, and about the big changes in C# 8 with the introduction of nullable reference types, along with the switch to make this the default in C# 10 and later. In an optional online-only section, you can learn how analyzers can help you write better code.
Chapter 7, Packaging and Distributing .NET Types, introduces the versions of .NET and includes tables showing which version introduced new library features in an online section. I will then present the .NET types that are compliant with .NET Standard and explain how they relate to C#. Throughout this chapter, you will learn how to write and compile code on any of the supported operating systems, including the Windows, macOS, and Linux variants. You will learn how to package, deploy, and distribute your own apps and libraries. In three optional online-only sections, you can learn how to use legacy .NET Framework libraries in .NET libraries, about the possibility of porting legacy .NET Framework code bases to modern .NET, and about source generators and how to create them.
Chapter 8, Working with Common .NET Types, discusses the types that allow your code to perform common practical tasks, such as manipulating numbers and text, storing items in collections, and, in an optional online-only section, working with a network using low-level types. You will also learn about regular expressions and the improvements that make writing them easier, as well as how to use source generators to improve their performance. Introduced with .NET 9 are new classes for Base64-encoded URLs, performing multiplication on big integers, ordered dictionaries, read-only sets, improvements to searching in strings, and being able to generate regular expressions on a partial property.
Chapter 9, Working with Files, Streams, and Serialization, covers interacting with a filesystem, reading and writing to files and streams, text encoding, and serialization formats like JSON and XML, including the improved functionality and performance of the System.Text.Json
classes. If you use Linux, then you will be interested in how to programmatically work with tar
archives, which you can learn about in an online-only section. .NET 9 (finally!) removes the dangerous legacy BinaryFormatter
serialization class and adds a class to export JSON schemas.
Chapter 10, Working with Data Using Entity Framework Core, explains reading and writing to relational databases, such as SQL Server and SQLite, using the object-relational mapping (ORM) technology named Entity Framework Core (EF Core). You will learn how to define entity models that map to existing tables in a database using Database First models. In three optional online-only sections, you can also learn how to define Code First models that can create tables and databases at runtime, how to insert, update, and delete data, and how to group multiple changes together using transactions.
Chapter 11, Querying and Manipulating Data Using LINQ, teaches you about Language INtegrated Queries (LINQ)—language extensions that add the ability to work with sequences of items and filter, sort, and project them into different outputs. This chapter includes LINQ methods introduced in .NET 6, like TryGetNonEnumeratedCount
and DistinctBy
, in .NET 7, like Order
and OrderDescending
, and in .NET 9, like CountBy
, AggregateBy
, and Index
. Optional online-only sections cover using multiple threads with parallel LINQ, working with LINQ to XML, and creating your own LINQ extension methods.
Chapter 12, Introducing Modern Web Development Using .NET, introduces you to the types of web projects that can be built using C# and .NET. You will also build an EF Core model to represent the database for a fictional organization named Northwind that will be used throughout the rest of the chapters in the book. You will learn how to efficiently manage NuGet package version using Central Package Management. Finally, you will be introduced to common web technologies like HTML and CSS.
Chapter 13, Building Websites Using ASP.NET Core, is about learning the basics of building websites with a modern HTTP architecture on the server side, using ASP.NET Core. You will learn how to implement the ASP.NET Core feature known as Blazor static Server-Side Rendering (SSR), which replaces the legacy Razor Pages technology for creating data-driven web pages. You will learn how to optimize static assets like JavaScript and stylesheets using a new method introduced with .NET 9, MapStaticAssets
. In two optional online-only sections, you’ll see how to customize the HTTP request and response pipeline, and you’ll gain insight into enabling HTTP/3 in your website project.
Chapter 14, Building Interactive Web Components Using Blazor, introduces how to build web user interface components using Blazor that can be executed either on the server side or on the client side inside the web browser. You will see how to build components that are easy to switch between the client and the server, with the new hosting model introduced with .NET 8.
Chapter 15, Building and Consuming Web Services, explains building backend REST architecture web services using the ASP.NET Core Minimal APIs. We will cover how to document them using the new Microsoft-implemented OpenAPI documentation generator. Introduced with .NET 9 is a hybrid cache that you will learn how to use to get the best of in-memory and distributed caching. Then we will see how to properly consume them using factory-instantiated HTTP clients. In two optional online-only sections, you will be introduced to advanced features, like health checks and adding security HTTP headers, and how Minimal APIs projects can use native ahead-of-time (AOT) compilation during the publishing process to improve startup time and memory footprint.
Epilogue describes your options for further study about C# and .NET.
Appendix, Answers to the Test Your Knowledge Questions, has the answers to the test questions at the end of each chapter. You can read the appendix at the following link: https://packt.link/LsQtz.