Records and tuples
A record is a type designed for data: the compiler gives it value equality, a readable ToString, the with expression and deconstruction. Tuples return several values from a method without creating a type.
18.1 The problem: same data, not equal
C#
var a = new Coupon("DIWALI50", 50);var b = new Coupon("DIWALI50", 50); Console.WriteLine(a == b);Console.WriteLine(a.Equals(b));Console.WriteLine(a); class Coupon(string code, int discount){ public string Code { get; } = code; public int Discount { get; } = discount;}Output
$ dotnet run Coupon.csFalseFalseCoupon- A class is a reference type: == checks whether both variables point to the same object.
- Two separate objects -> False, even with identical data.
- ToString prints only the class name, useless in logs.
18.2 Fix: a positional record
- record Coupon(string Code, int Discount); replaces the whole class.
- The compiler generates the constructor, init-only properties, Equals/GetHashCode, ==, ToString and Deconstruct.
- Records compare by value: both checks become True.
C#
var a = new Coupon("DIWALI50", 50);var b = new Coupon("DIWALI50", 50); Console.WriteLine(a == b);Console.WriteLine(a.Equals(b));Console.WriteLine(a); record Coupon(string Code, int Discount);Output
$ dotnet run Coupon.csTrueTrueCoupon { Code = DIWALI50, Discount = 50 }- Step 1
1.You write: record Coupon(string Code, int Discount);
- Step 2
2.Compiler adds a constructor + init-only Code, Discount
- Step 3
3.Adds Equals, GetHashCode and == comparing values
- Step 4
4.Adds ToString: Coupon { Code = DIWALI50, Discount = 50 }
- Step 5
5.Adds Deconstruct and support for with
18.3 with: copy instead of change
- Positional record properties are init-only: diwali.Discount = 75 -> CS8852.
- diwali with { Discount = 75 } creates a copy with only Discount changed; the original stays 50.
C#
var diwali = new Coupon("DIWALI50", 50);var bigger = diwali with { Discount = 75 }; Console.WriteLine(diwali);Console.WriteLine(bigger); record Coupon(string Code, int Discount);Output
# if we write: diwali.Discount = 75;$ dotnet run Coupon.csCoupon.cs(2,1): error CS8852: Init-only property 'Coupon.Discount' can only be assigned in an object initializer ...The build failed. Fix the build errors and run again.18.4 Deconstruction
- var (code, discount) = coupon; pulls the values into separate variables.
- Uses the compiler-generated Deconstruct; every positional record gets it automatically.
C#
var coupon = new Coupon("DIWALI50", 50); var (code, discount) = coupon;Console.WriteLine($"{code}: Rs {discount}"); record Coupon(string Code, int Discount);Output
$ dotnet run Coupon.csDIWALI50: Rs 5018.5 record struct: a value type
- record (record class) is a reference type; record struct is a value type for small data.
- readonly record struct GeoPoint: properties cannot change.
- Without readonly, record struct properties are mutable.
C#
var p1 = new GeoPoint(28.61, 77.20);var p2 = new GeoPoint(28.61, 77.20); Console.WriteLine(p1 == p2);Console.WriteLine(p1); readonly record struct GeoPoint( double Lat, double Lng);Output
$ dotnet run GeoPoint.csTrueGeoPoint { Lat = 28.61, Lng = 77.2 }18.6 Tuple: return two values
- (decimal Min, decimal Max) is a tuple return type with named elements.
- Deconstruct it: var (min, max) = MinMax(prices); or access by name: range.Max.
- Tuples are compared by value too.
C#
decimal[] prices = [499m, 1299m, 89m]; var (min, max) = MinMax(prices);Console.WriteLine($"Min {min}, Max {max}"); var range = MinMax(prices);Console.WriteLine(range.Max);Console.WriteLine(range == (89m, 1299m)); static (decimal Min, decimal Max) MinMax( decimal[] items) => (items.Min(), items.Max());Output
$ dotnet run Prices.csMin 89, Max 12991299True18.7 Comparison and when to use which
| Type | Kind | Equality | Use for |
|---|---|---|---|
| class | Reference | Reference (same object?) | Behaviour + identity: OrderService, bank account |
| record (class) | Reference | Value (same data?) | Data carriers: DTO, coupon |
| readonly record struct | Value | Value | Small values: GeoPoint |
| tuple | Value (ValueTuple) | Value | Local / private temporary values |
- Step 1
1.Behaviour and identity matter? -> class
- Step 2
2.Carries data (DTO, coupon)? -> record
- Step 3
3.Small value like coordinates? -> readonly record struct
- Step 4
4.Temporary values inside a method? -> tuple
In a public API use a record, not a tuple
18.8 Checkpoint: with creates a new object
C#
var a = new Coupon("DIWALI50", 50);var b = a with { }; Console.WriteLine(a == b);Console.WriteLine(ReferenceEquals(a, b)); record Coupon(string Code, int Discount);Output
$ dotnet run Coupon.csTrueFalse18.9 Common mistakes
- Returning an unnamed tuple from a public method: order.Item2 tells the reader nothing. Use a record (OrderSummary(Id, Total, Items)).
- Thinking records are fully immutable: positional properties are init-only, but a List inside can still be changed.
- Thinking record equality is deep: it is shallow; two records with separate lists holding the same items are not equal.
- Using records for EF Core entities: an entity has identity and should not be compared by data.
C#
var order = GetOrder();Console.WriteLine(order.Total); static OrderSummary GetOrder() => new("ORD-101", 2499m, 3); record OrderSummary( string Id, decimal Total, int Items);18.10 5 rules for records and tuples
- Data: record; behaviour: class.
- Keep records immutable; change with with.
- Small value objects: readonly record struct.
- Tuples only for local or private work.
- EF Core entities: not records.
18.11 Interview tips
- A record is a type for data with compiler-generated value equality, readable ToString and with.
- record class = reference type, record struct = value type; positional records are immutable by default (init-only).
- with always creates a new object: == True but ReferenceEquals False.
- Named tuples for returning multiple values locally; a record in a public API.