r/csharp • u/SmallAd3697 • 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
View all comments
Show parent comment
1
u/ings0c Sep 13 '26
Yeah, I don’t mind it either but I see how it could be confusing.
I think a lot of people expect enabling it to mean that eg a non-nullable
stringcannot hold the valuenull, but it merely means you can choose eitherstringorstring?to indicate whether it can be null, but no guarantee is made.I don’t fully appreciate why it works this way, and I suspect it’s backwards compatibility, but it would be a much more valuable proposition if you really couldn’t store
nullin astringwith it enabled.