Skip to content

Positional records

Context: Positional records use a concise syntax with constructor parameters. The compiler generates init properties and a deconstructor.

public record Product(string Name, decimal Price, int Stock);
  • Properties: public string Name { get; init; }, etc.
  • Constructor: Product(string Name, decimal Price, int Stock)
  • Deconstructor: public void Deconstruct(out string Name, out decimal Price, out int Stock)
var product = new Product("Laptop", 999.99m, 10);
var (name, price, stock) = product; // deconstruction
Console.WriteLine(name); // Laptop

API response contracts: Define a positional record for a REST API response (e.g., public record ApiResponse<T>(bool Success, T Data, string ErrorMessage)). Deconstruction makes it easy to extract values.

Example: In Minimal APIs with .NET 6+, you can return records directly.