❔ Benefit of ref structs

Hey folks!

I've been trying for a bit to understand the purpose of using a ref struct over a regular struct. I understand that it can only live on the stack, which is enforced via a set of usage rules, but regular structs could do that too if used properly.

I asked my good old friend chatgpt to help me understand it, and the answer I got was basically that the compiler is able to optimize the usage of ref structs better given its restrictions. Is this accurate? Is it the only benefit?

I'm used to concepts like the out keyword where its usage restrictions (must be populated by return, cannot be read) come as a tradeoff for visible benefits (inline variable declaration in a method call), so I guess I was expecting something similar in this case, specifically something that can only be done with ref structs.
Was this page helpful?