Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
C#

C#: Fix a Record Struct That Silently Copies When You Meant to Mutate It in Place

- 03.10.26 - ErcanOPAK

🔧 The Record Struct Update That Changed a Copy, Not the Original

A record struct looks and behaves like a record in almost every way that matters for readability – value equality, a generated ToString, a with expression for non-destructive updates – but it’s still a struct underneath, which means it’s copied by value every time it’s passed to a method, stored in a collection’s indexer result, or read through a property that doesn’t return by reference. Code that calls a with expression or sets a property on a record struct pulled out of a list or dictionary is usually mutating a temporary copy of that element, not the element actually sitting in the collection, and the change simply vanishes.

🔎 The Problem

public record struct ScoreEntry(string Player, int Points);

var scores = new List<ScoreEntry>
{
    new("Alice", 10),
    new("Bob", 7)
};

// This compiles, runs, and does... nothing useful. foreach's
// loop variable is a COPY of the struct, so updating it updates
// only that copy, never the entry actually sitting in the list.
foreach (var entry in scores)
{
    entry.Points += 1;
}

Console.WriteLine(scores[0].Points); // still 10, not 11

✅ Fix: Mutate Through an Index, or Replace the Element Entirely

  • Accessing the list by index (scores[i] = scores[i] with { Points = scores[i].Points + 1 }) and reassigning the element works because it writes a new value back into the list’s actual storage slot, rather than mutating a disposable copy that was only ever a loop variable.
  • Using a for loop with an index instead of foreach, and applying the with expression or property update directly against list[i], keeps every change aimed at the collection’s real storage rather than at a temporary value sitting on the stack.
  • Switching to an ordinary record class (not a record struct) removes this entire category of mistake when a given type is going to be stored in collections and updated frequently, since a class reference is shared rather than copied every time it’s read out.

⚠️ Why the Compiler Doesn’t Flag This

  • foreach deliberately uses a read-only-feeling copy for value types for safety reasons unrelated to this scenario (iterator invalidation), and the compiler has no way to know the developer’s intent was to mutate the original rather than inspect a copy.
  • A record struct’s with expression is specifically designed to produce a new value rather than modify in place – which is exactly the correct, by-design behavior for a value type, even though it reads, at the call site, almost identically to how it would look updating a reference type’s property.

A record struct with expression doesn’t update anything – it hands back a brand new value, and whether that new value replaces anything real depends entirely on what you do with it next.

— C# Best Practice

Related posts:

How to calculate difference (number of days) between two dates in C#

How to calculate difference (number of days) between two dates in C#

DateTime.Now Can Break Distributed Systems

DateTime.Now Can Break Distributed Systems

C#: Master Exception Handling for Robust Applications

C#: Master Exception Handling for Robust Applications

Views: 12
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

CSS: Fix a Text-Overflow Ellipsis That Never Appears Because the Container Has No Fixed Width
The AI Prompt That Checks a Cron Expression and Catches Schedules That Will Never Actually Run

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,080 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 869 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 672 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 585 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com