In C#, what does the compiler generate for a `record struct` that a plain `struct` with the same fields does not get?
answer
- value equality written for you
- an interface the plain struct lacks
- operators a plain struct cannot use
- type name, braces, member list
basics
~20 sA C# record struct gets compiler-written IEquatable<T>.Equals, == and !=, a field-based GetHashCode and a member-listing ToString, plus properties and Deconstruct when positional. A plain struct only inherits ValueType's Equals, GetHashCode and type-name ToString, and has no ==.
solid answer
~50 sDeclaring `record struct Price(decimal Amount, string Currency)` (C# 10) makes the compiler add a public property and a primary-constructor parameter for each positional parameter, a `Deconstruct` method, and a full set of equality members: the struct implements `IEquatable<Price>` with a public `Equals(Price)` that compares every instance field through `EqualityComparer<T>.Default`, an `Equals(object)` override that delegates to it, a matching `GetHashCode`, and `==` and `!=` operators. It also writes a `ToString` that prints `Price { Amount = 101.25, Currency = USD }` through a private `PrintMembers` method. A plain struct with the same fields gets none of that: it inherits `ValueType.Equals(object)`, which takes a boxed argument and compares raw bytes when it can, and otherwise, as with a string field, reads the fields by reflection; its `ToString` returns the type name; and `p == q` does not compile (CS0019).
code
csharp · 19 linesusing System.Globalization;
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
var a = new Price(101.25m, "USD");
var b = new Price(101.25m, "USD");
Console.WriteLine(a == b); // synthesized operator ==
Console.WriteLine(a.Equals(b)); // IEquatable<Price>.Equals(Price)
Console.WriteLine(a); // synthesized ToString
var (amount, currency) = a; // synthesized Deconstruct
Console.WriteLine($"{amount} {currency}");
var p = new PlainPrice { Amount = 101.25m, Currency = "USD" };
var q = new PlainPrice { Amount = 101.25m, Currency = "USD" };
Console.WriteLine(p.Equals(q)); // inherited ValueType.Equals(object)
Console.WriteLine(p); // inherited ValueType.ToString
// Console.WriteLine(p == q); // error CS0019: no == on a plain struct
record struct Price(decimal Amount, string Currency);
struct PlainPrice { public decimal Amount; public string Currency; }go deeper
Recall the list: IEquatable<T>.Equals, Equals(object), GetHashCode, == and !=, ToString, and for a positional one properties and Deconstruct. Then say a plain struct has no == at all.
Explain that the synthesized Equals compares every instance field through EqualityComparer<T>.Default, and contrast it with ValueType.Equals, which boxes and may fall back to reflection.
Point out the consequences of field-wise comparison: arrays and lists compare by reference, private fields count, and generic collections avoid boxing because IEquatable<T> is implemented.
Frame record struct as the cheap way to give a small value type correct, fast equality and readable logs, and say when a hand-written struct is still warranted.
## What `record struct` declares C# 10 added the `record` modifier to structs. `record struct Price(decimal Amount, string Currency);` is still a **value type**: it derives implicitly from `System.ValueType`, it is copied on assignment, and it obeys every rule an ordinary struct obeys. What the `record` modifier adds is a set of **compiler-synthesized members** — methods and properties the compiler writes into the type unless you declare a matching one yourself. The parameter list in parentheses makes it a **positional** record struct. Without a parameter list (`record struct Price { ... }`) it is a nominal record struct: it still gets the equality and printing members, but no generated properties, primary constructor or `Deconstruct`. ## The members, side by side | Member | `record struct Price(...)` | plain `struct PlainPrice` with the same fields | |---|---|---| | `IEquatable<Price>` | implemented, with public `Equals(Price)` | not implemented | | `Equals(object)` | override that unboxes and calls `Equals(Price)` | inherited `ValueType.Equals` | | `GetHashCode()` | override combining every field's hash | inherited `ValueType.GetHashCode` | | `==` and `!=` | synthesized, calling `Equals(Price)` | none: `p == q` is error CS0019 | | `ToString()` | `Price { Amount = 101.25, Currency = USD }` | the type name, `PlainPrice` | | properties | one public property per positional parameter | only what you write | | `Deconstruct` | one `out` parameter per positional parameter | none | | parameterless constructor | zero-initializes every field | zero-initializes every field | ## Equality: field by field, with no boxing The synthesized `Equals(Price other)` returns `true` when, for **every instance field** of the struct, `EqualityComparer<TField>.Default.Equals(thisField, otherField)` is `true`. "Every instance field" means the backing fields of the positional properties and any other instance field or auto-property you add, private ones included. Some consequences: - a `string` field compares by content, ordinal and case-sensitive, because that is what `string.Equals` does; - a `decimal` field compares numerically, so `1.0m` and `1.00m` are equal; - an array or `List<T>` field compares by **reference**, because those types do not override `Equals`; - a `double` field holding `NaN` equals another `NaN`, because `double.Equals` treats `NaN` as equal to itself, unlike the `==` operator on doubles. Because the struct implements `IEquatable<Price>`, generic code — `HashSet<Price>`, `Dictionary<Price, T>`, `List<Price>.Contains` — calls `Equals(Price)` directly, with no boxing. A plain struct has only the inherited `ValueType.Equals(object)`. Calling it boxes the argument; the runtime then compares the raw bytes when the struct holds no references and meets its other fast-path conditions, and otherwise reads each field through reflection. `PlainPrice` has a `string` field, so it takes the reflection path. The result is usually the same verdict, at a much higher cost — and the plain struct still has no `==` at all. ## Printing and deconstruction The synthesized `ToString` writes the type name, `" { "`, then calls a private `PrintMembers(StringBuilder)` that appends `Name = value` for each public field and readable property, separated by commas, then `" }"`. You can declare your own `ToString` or `PrintMembers` with the expected signature. `Deconstruct` lets you write `var (amount, currency) = price;` and use the record struct in positional patterns such as `price is (> 0m, "USD")`. ## What a record struct does not get Compared with a **record class**, a record struct has: 1. no `EqualityContract` property, because a struct cannot be inherited, so there is no run-time type to compare; 2. no null checks in `Equals` or `==`, because a non-nullable struct value is never null; 3. no synthesized copy constructor or `Clone` method — a `with` expression copies the value and then sets the listed members, and a member named `Clone` is a compile error; 4. no `virtual` members, since nothing can override them. ## What interviewers listen for - that `record struct` and `struct` have the **same** equality definition — same type, same field values — and differ in **who implements it**: the compiler, per type, instead of `ValueType` at run time; - that a plain struct has **no `==`** unless you write one; - that the synthesized `ToString` is genuinely useful in logs and test failures, where a plain struct prints only its name.
- Can a C# record struct have no parameter list, and what does it then lose?Yes. `record struct Price { public decimal Amount { get; init; } }` is a nominal record struct. It still gets `IEquatable<Price>`, `Equals`, `GetHashCode`, `==`, `!=` and the member-listing `ToString`. It loses only what the parameter list drives: the generated properties, the primary constructor and `Deconstruct`.
- Does a record struct's synthesized Equals compare the contents of an `int[]` member?No. It calls `EqualityComparer<int[]>.Default.Equals`, and arrays do not override `Equals`, so two instances are equal only when they hold the same array object. Two arrays with the same elements make the record structs unequal. To compare contents, declare your own `Equals(R other)` using `SequenceEqual` and a `GetHashCode` that agrees with it.
- A plain struct already supports == on its fields.
- A record struct is a class with value equality, allocated on the heap.
- A record struct gets an EqualityContract and a Clone method like a record class.
- The generated Equals compares only the positional properties.
- Record struct equality compares strings ignoring case.