Chapter 9. Examining Recent C# Language Highlights
The C# programming language is continually evolving. Earlier chapters discussed subjects from C# 1 through C# 8. The exception is pattern matching in Chapter 8, where some of the patterns were introduced in C# 9. This chapter focuses primarily on C# 9, the exception being the subject of Recipe 9.9, which is a C# 8 feature.
A central concept in this chapter is immutability—the ability to create and operate on types that don’t change. Immutability is important for safe multithreading as well as for the cognitive relief of knowing that code you’ve passed a type to won’t change (mutate) the contents of the object.
The example scenario is working with addresses, such as a mailing address or a shipping address. In many contexts, an address is a value, meaning that it doesn’t have an identity. The address exists as a set of data (value) associated with an entity such as a customer or company. On the other hand, an entity does have an identity, normally modeled as an ID field in a database. Because we’re treating it as a value, address becomes a useful candidate for immutability because we don’t want the value to change once it’s set.
One interesting feature of C# 9 is called module initialization. Think about how we use constructors to initialize types or
Main to initialize an application. Module initialization lets you write initialization code at the scope of an assembly, and you’ll see how that works.
Another C# 9 theme is code simplification. ...
Get C# Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.