Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
C#

C#: Fix a Record Struct Losing Equality After Adding a Field

- 26.11.26 - ErcanOPAK

🔧 C#: Fix a Record Struct Losing Equality After Adding a Field

A record struct’s compiler-generated Equals and GetHashCode compare every field the record declares, generated fresh at compile time from whatever the type’s member list looks like right then – there’s no runtime reflection involved, so adding a new field to the record regenerates both methods automatically and correctly. The actual trap is a record struct that implements IEquatable<T> or overrides Equals manually for a special case (ignoring a cache field, say) – adding a new field later doesn’t touch that hand-written method at all, so the new field silently never participates in equality while every auto-generated comparison elsewhere in the codebase still works, hiding the gap.

🔎 The Problem

public readonly record struct Money
{
    public decimal Amount { get; init; }
    public string Currency { get; init; }

    public bool Equals(Money other) =>
        Amount == other.Amount && Currency == other.Currency;
    // hand-written override, correct for 2 fields
}

// Later: a RoundingMode field is added to the record
// public RoundingMode RoundingMode { get; init; }
// The hand-written Equals above is NEVER updated - two Money values
// with different RoundingMode now compare as equal silently

✅ The Fix

  • Delete the hand-written Equals/GetHashCode entirely unless you have a specific, documented reason to exclude a field from equality, and let the compiler regenerate both from the full current field list every time the record changes.
  • If a field genuinely should be excluded from equality (a cached, derived value, for instance), say so explicitly in a comment right next to the custom Equals implementation, so the next person adding a field sees the warning before they assume equality is still automatic.
  • Add a unit test that constructs two instances differing only in the newest field and asserts they are NOT equal, specifically to catch exactly this class of silently-stale custom equality the next time a field gets added.

🚨 Why This Is Dangerous

Silently broken equality doesn’t throw anywhere – it just makes two genuinely different values compare as equal, which can corrupt a HashSet, a Dictionary key, or a deduplication step with no exception to point at and no obvious place to start looking.

The compiler only regenerates the equality it’s responsible for – the moment you write your own Equals by hand, every new field is on you to remember.

— C# Best Practice

Related posts:

C#: Why String.Format Culture Mismatches Silently Corrupt Numbers on a Different Locale Machine

C#: Why String.Format Culture Mismatches Silently Corrupt Numbers on a Different Locale Machine

C#: Why a Sealed Override Still Gets Hidden by a Subclass Using the new Keyword

C#: Why a Sealed Override Still Gets Hidden by a Subclass Using the new Keyword

Why async Code Can Still Block Threads

Why async Code Can Still Block Threads

Views: 9
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Docker: Fix a Volume Mount Hiding Files Baked Into the Image
HTML5: Fix an Output Element Never Updating From a Range Input

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 629 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com