Chapter 2: C# records, init, and immutable collections

In this chapter

  • 2.1 record class: the book’s main tool for immutable reference types
    • Two declaration styles
    • Value equality: the key difference between records and classes
  • 2.2 Init-only properties and required
  • 2.3 With expressions: creating modified versions
    • Nested updates
  • 2.4 The shallow immutability trap
    • Switching to IReadOnlyList<T>
  • 2.5 Choosing immutable collections
    • The ImmutableArray<T> default trap
    • Collection fields and value equality
    • FrozenDictionary: build once, query many times
  • 2.6 Record structs and readonly record structs
    • When to use a record struct
    • Record structs are mutable by default
    • Avoid mutable structs
  • 2.7 Chapter review

This chapter is not available in the free preview. Purchase the book to read the complete edition now.

Buy this book