{"id":2989,"date":"2024-09-03T07:25:41","date_gmt":"2024-09-03T06:25:41","guid":{"rendered":"https:\/\/contentlabstudy.com\/soft\/?p=2989"},"modified":"2026-09-24T21:16:38","modified_gmt":"2026-09-24T20:16:38","slug":"c-record-methods","status":"publish","type":"post","link":"https:\/\/contentlabstudy.com\/soft\/c-record-methods\/","title":{"rendered":"C# Record Methods"},"content":{"rendered":"<p>A C# <strong>record<\/strong> is a reference type (or <code>record struct<\/code>) built for value-shaped data: equality by contents, a concise constructor, and non-destructive copy with <code>with<\/code>. Use records for messages, query results, and value objects. Do not use them as EF Core entities with identity unless you know the tracking implications.<\/p>\n<h2>Positional vs explicit<\/h2>\n<pre><code>public sealed record Money(decimal Amount, string Currency);\n\nvar price = new Money(10m, \"USD\");\nvar tax = price with { Amount = 1.5m };\n<\/code><\/pre>\n<p><code>Money<\/code> equality is by amount and currency, not by reference. <code>decimal<\/code> already makes <code>0.1m + 0.2m<\/code> exact. Records do not choose scale or rounding (two places, away from zero or to even). Do that at the boundary.<\/p>\n<pre><code>var price = new Money(10m, \"USD\");\nvar tax = price with { Amount = 1.5m };\nbool same = price == new Money(10m, \"USD\");\nvar (amount, currency) = price;<\/code><\/pre>\n<h2>Members that matter<\/h2>\n<ul>\n<li><strong>Primary constructor parameters<\/strong> become public init properties unless you declare otherwise.<\/li>\n<li><strong><code>with<\/code><\/strong> copies the record\u2019s own fields and sets the ones you name. It is shallow: a nested <code>List&lt;T&gt;<\/code> or class is still the same reference.<\/li>\n<li><strong>Synthesized members:<\/strong> <code>Equals<\/code>, <code>GetHashCode<\/code>, <code>==<\/code> and <code>!=<\/code>, <code>ToString<\/code> (fine for logs, not for UI), <code>Deconstruct<\/code>, <code>PrintMembers<\/code>, and the copy <code>with<\/code> uses.<\/li>\n<li><strong>Inheritance:<\/strong> <code>record<\/code> can inherit record. Equality includes runtime type. A <code>Dog<\/code> is not equal to an <code>Animal<\/code> with the same fields.<\/li>\n<\/ul>\n<h2>record class vs record struct vs class<\/h2>\n<p><code>record class<\/code> (default) is a heap object with value equality \u2014 cheap to pass around, still a reference. <code>record struct<\/code> is a value type; copies are copies, large structs in collections hurt. Ordinary <code>class<\/code> is the right default for entities with identity and lifecycle.<\/p>\n<h2>What breaks it<\/h2>\n<p>A record with public setters is a dictionary key, then a property changes. <code>ContainsKey<\/code> misses because the hash changed. Keep the positional <code>Money<\/code> above, whose properties are <code>init<\/code>.<\/p>\n<pre><code>public record MutableMoney\n{\n    public decimal Amount { get; set; }\n    public string Currency { get; set; } = \"\";\n}\n\nstatic bool LookupAfterMutate()\n{\n    var money = new MutableMoney { Amount = 10m, Currency = \"USD\" };\n    var map = new Dictionary&lt;MutableMoney, string&gt; { [money] = \"price\" };\n    money.Amount = 11m;\n    return map.ContainsKey(money);\n}<\/code><\/pre>\n<p><code>with<\/code> on a nested list still shares the list. <code>b.Lines.Add<\/code> changes <code>a.Lines<\/code>.<\/p>\n<pre><code>sealed record Basket(List&lt;string&gt; Lines);\n\nstatic bool SharedList()\n{\n    var a = new Basket(new List&lt;string&gt; { \"a\" });\n    var b = a with { };\n    b.Lines.Add(\"b\");\n    return a.Lines.Contains(\"b\");\n}<\/code><\/pre>\n<h2>Pitfalls<\/h2>\n<ul>\n<li>Mutable record properties (<code>set<\/code> instead of <code>init<\/code>) plus using them as dictionary keys.<\/li>\n<li>EF Core: records can work, but identity-map + value equality is easy to get wrong. Prefer classes for aggregates.<\/li>\n<li>Huge nested records copied with <code>with<\/code> in a hot loop.<\/li>\n<\/ul>\n<p>See <a href=\"\/soft\/domain-driven-design-principles\/\">value objects<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A C# record is a reference type (or record struct) built for value-shaped data: equality by contents, a concise constructor, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2989","post","type-post","status-publish","format-standard","hentry","category-software-development"],"_links":{"self":[{"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/posts\/2989","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/comments?post=2989"}],"version-history":[{"count":4,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/posts\/2989\/revisions"}],"predecessor-version":[{"id":3254,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/posts\/2989\/revisions\/3254"}],"wp:attachment":[{"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/media?parent=2989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/categories?post=2989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/contentlabstudy.com\/soft\/wp-json\/wp\/v2\/tags?post=2989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}