In a C# `record struct`, how do you make equality ignore a string member's case, and which equality members can you not declare?
answer
- one strongly typed method to replace
- the hash must agree
- operators stay compiler-owned
- duplicate member diagnostic
basics
~10 sDeclare a public Equals(R other) and override GetHashCode in the record struct; the synthesized ==, != and Equals(object) then call your Equals. Declaring operator ==, operator != or Equals(object) yourself is error CS0111.
solid answer
~30 sA record struct has one replaceable comparison: the public `Equals(R other)` that implements `IEquatable<R>`. Declare it — `public readonly bool Equals(Tick other) => string.Equals(Symbol, other.Symbol, StringComparison.OrdinalIgnoreCase) && Seq == other.Seq;` — and the compiler stops generating its own. The synthesized `==` and `!=` call `Equals(R)`, and the synthesized `Equals(object)` unboxes and calls it too, so every path agrees. Override `GetHashCode` with the same rule (`StringComparer.OrdinalIgnoreCase.GetHashCode(Symbol)`), or hash-based collections break; declaring `Equals` alone gives warning CS8851. `Equals(R)` must be public (CS8873 otherwise). Declaring `operator ==`, `operator !=` or `Equals(object)` yourself is error CS0111, because those three are always synthesized.
code
csharp · 15 linesvar t = new Tick("eurusd", 42);
var u = new Tick("EURUSD", 42);
Console.WriteLine(t == u); // == calls the Equals(Tick) below
Console.WriteLine(t.Equals((object)u)); // so does the synthesized Equals(object)
Console.WriteLine(new HashSet<Tick> { t }.Contains(u)); // and EqualityComparer<Tick>.Default
record struct Tick(string Symbol, long Seq)
{
public readonly bool Equals(Tick other) =>
string.Equals(Symbol, other.Symbol, StringComparison.OrdinalIgnoreCase)
&& Seq == other.Seq;
public override readonly int GetHashCode() =>
HashCode.Combine(StringComparer.OrdinalIgnoreCase.GetHashCode(Symbol), Seq);
}go deeper
Remember that you customize a record struct's equality by declaring a public Equals taking the record struct type, plus GetHashCode.
Explain why == and Equals(object) cannot be declared (CS0111) and how they route through Equals(R), and why GetHashCode must use the same rule.
Spot the explicit-interface trap and hash mismatches in review, and judge when the field-wise default is already right, such as decimal scale or NaN.
Decide whether a value with non-default equality should be a record struct at all, or a plain struct whose whole contract is written by hand.
## The equality members and who owns them A record struct gets four equality members from the compiler. Only some of them may be replaced: | Member | Synthesized | May you declare it? | If you try anyway | |---|---|---|---| | `public bool Equals(R other)` | yes, unless you declare it | **yes** — must be public | a non-public one is error CS8873 | | `public override int GetHashCode()` | yes, unless you declare it | **yes** | — | | `public override bool Equals(object? obj)` | always | **no** | error CS0111 | | `operator ==` and `operator !=` | always | **no** | error CS0111 | The design is deliberate: there is exactly **one** comparison to customize, and the compiler routes every other entry point through it. The synthesized `==` is `r1.Equals(r2)`, the synthesized `!=` is `!(r1 == r2)`, and the synthesized `Equals(object)` is `obj is R temp && Equals(temp)`. No path can disagree with another. ## Replacing the comparison, step by step Suppose a market-data feed sends `"eurusd"` from one venue and `"EURUSD"` from another, and `record struct Tick(string Symbol, long Seq)` should treat them as the same tick. 1. Declare `public bool Equals(Tick other)` with the comparison you need — here `string.Equals(..., StringComparison.OrdinalIgnoreCase)` plus the sequence number. 2. Override `GetHashCode` so that equal values hash equally — `HashCode.Combine(StringComparer.OrdinalIgnoreCase.GetHashCode(Symbol), Seq)`. 3. Mark both `readonly`. The synthesized members of a record struct are `readonly`; marking yours the same keeps them callable without a defensive copy on a `readonly` field or an `in` parameter. Omitting `readonly` still compiles. 4. Leave `==`, `!=` and `Equals(object)` alone. They now use your method. The snippet below shows `==`, `Equals(object)` and `HashSet<Tick>.Contains` all agreeing that the two ticks are equal. ## The hash must agree with the comparison `HashSet<T>` and `Dictionary<TKey, TValue>` first pick a bucket by hash code, then call `Equals` only inside that bucket. If `"eurusd"` and `"EURUSD"` compare equal but hash differently, lookups fail at random. The compiler helps with one direction: declaring `Equals(R)` without `GetHashCode` gives warning **CS8851**, "'Tick' defines 'Equals' but not 'GetHashCode'". Declaring only `GetHashCode` produces no warning in the C# 14 compiler, even though the record structs proposal describes the warning as symmetric. ## A trap: an explicit interface implementation Writing `bool IEquatable<Tick>.Equals(Tick other) => ...` compiles, but it does **not** replace the synthesized public `Equals(Tick)`. The result is two comparisons: code that calls through the interface (some generic code) uses yours, while `==` and the public `Equals` keep the field-wise default. Declare the public method instead. ## What the default comparison already does Before customizing, check whether the generated comparison is already right. It calls `EqualityComparer<T>.Default.Equals` on each instance field, so: - `string` members compare **ordinally and case-sensitively**; - `decimal` members compare numerically, `1.0m` equal to `1.00m`; - `double` members treat `NaN` as equal to `NaN`, since `double.Equals` does, which keeps equality reflexive for hashing; - array and `List<T>` members compare **by reference**. Customizing is warranted for the case-insensitive key, for comparing a collection member's contents, and for leaving a member out of equality, such as a cached display string. ## Leaving a member out of equality The synthesized comparison covers every instance field, so a member that is not part of the value's identity — a received-at timestamp, a cached formatted string — still makes two otherwise identical ticks unequal. The fix is the same single method: 1. declare `public bool Equals(Tick other)` comparing only the identity members; 2. declare `GetHashCode` hashing exactly the same members, no more; 3. remember that the synthesized `ToString` still prints the excluded member, which is usually what you want in logs. A hash that includes a member `Equals` ignores is the worst version of the bug: equal values land in different buckets, and the set holds both. ## What you can still override elsewhere `ToString`, the private `PrintMembers(StringBuilder)` and `Deconstruct` can also be declared with the expected signature. Unlike a record class, a record struct has no `EqualityContract` to override, because nothing can derive from a struct.
- Why is a NaN member equal to itself in a C# record struct when `double.NaN == double.NaN` is false?The synthesized `Equals` compares a `double` field through `EqualityComparer<double>.Default`, which calls `double.Equals`. `double.Equals` treats `NaN` as equal to `NaN` so that equality stays reflexive and the value can be found in a hash set. The `==` operator on two doubles follows IEEE 754 and returns false. The record struct's `==` calls `Equals`, so it reports the two instances as equal.
- What happens if a record struct implements `IEquatable<R>.Equals` explicitly instead of declaring a public `Equals(R)`?It compiles, and the compiler still synthesizes the public `Equals(R)`. Calls through the `IEquatable<R>` interface use your explicit method, while `==`, `!=`, `Equals(object)` and direct calls use the synthesized field-wise one. The type now has two notions of equality. Declare the public method instead.
- Overload operator == in the record struct to change how it compares.
- Override Equals(object) and the other equality members will follow it.
- Changing Equals alone is enough; GetHashCode adapts automatically.
- An explicit IEquatable<T>.Equals implementation replaces the synthesized Equals.