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 Ref Readonly Parameter Silently Copying on Call

- 25.11.26 - ErcanOPAK

🔧 C#: Fix a Ref Readonly Parameter Silently Copying on Call

C# 12’s ref readonly parameters are meant to pass a large struct by reference without letting the callee mutate it, but they only avoid a copy when the caller passes a value with a stable storage location — a variable. Passing the result of a method call or a property instead gives the compiler nothing stable to reference, so it silently creates a temporary copy to satisfy the parameter anyway, fully legal and uncomplained-about by default, which defeats the entire performance purpose of using ref readonly in the first place.

🔎 The Problem

void Transform(ref readonly Matrix4x4Big m) { /* ... */ }

Transform(GetMatrix());     // GetMatrix() returns a struct by value -
                             // compiler creates a temp copy to pass by ref,
                             // the copy ref readonly was meant to avoid

var m = GetMatrix();
Transform(in m);             // no copy - m is a real variable

✅ Fix: Pass an Actual Variable, and Turn On the Warning That Catches This

  • Enable the compiler warning that flags a ref readonly argument that isn’t a variable, so the compiler surfaces the silent copy instead of staying quiet about it.
  • Store a method or property result in a local variable first, then pass that variable by ref readonly, whenever the call site is a hot path where the whole point of the parameter was avoiding a struct copy.
  • Benchmark the specific call site before assuming ref readonly bought anything — if every call site actually passes an expression rather than a variable, the parameter is providing mutation safety but none of the copy-avoidance performance it was added for.

🚨 Why This Is Dangerous

  • A hot-path struct still being copied on every call despite being passed ref readonly can mean a performance optimization silently did nothing, and without the specific compiler warning enabled, nothing in a normal build will tell you that.

ref readonly promises not to mutate your struct — it never promised not to copy it, if that’s the only way the call site gave it to work with.

— C# Best Practice

Related posts:

C#: Why async void Event Handlers Silently Swallow Exceptions (And How to Catch Them Before Producti...

C#: Why async void Event Handlers Silently Swallow Exceptions (And How to Catch Them Before Producti...

C#: Master Exception Handling with Try-Catch

C#: Master Exception Handling with Try-Catch

C#: Why Two Identical LINQ Queries Return Rows in a Different Order Each Time

C#: Why Two Identical LINQ Queries Return Rows in a Different Order Each Time

Views: 7
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

JavaScript: Fix a Proxy Trap Missing Updates From a Nested Object
Asp.Net Core: Fix a Hosted Service Starting Before Its Dependency

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,079 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# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 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 584 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