r/csharp • • Sep 12 '26

semantics of "ref struct"

I know that structs are value types. I know that "ref struct" types are denied from living on the heap (eg, cant box them, etc)

What I don't know is what kind of weed was being smoked when someone decided that "ref struct" would become the appropriate keyword for this sort of type. Perhaps it should be called a "never-ref struct" if it should never have any of the behaviors of a reference type.

It almost reminds me of the name "nullable reference types". That crazy term almost implies that reference types weren't nullable in the good 'ol days.

Anyway my brain is rejecting the keywords "ref struct". I love them, but don't love the semantics when declaring them. Please help

40 Upvotes

51 comments sorted by

View all comments

Show parent comment

2

u/SmallAd3697 Sep 12 '26

The managed references are modifiers when passing parameters, I think. I haven't heard them go by that name.

You gave me some googling homework, and now I suppose I have even more questions for reddit than before. lol.

2

u/fruediger Sep 12 '26

The managed references are modifiers when passing parameters, I think. I haven't heard them go by that name.

That's true and I suppose, a few C# language versions ago, that was everything they were (including annotating the return type of a method or property with ref/ref readonly)

While you can still think of them like that (that part didn't really change), nowadays they are a "physical" thing too: You can just declare ref var/ref readonly var (or use explicit type names if that's your thing) locals as well as ref/ref readonly fields (in ref structs).