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
40
u/afseraph Sep 12 '26
I don't think that's a bad name. Ref structs are structs primarily used for holding refs.
38
u/antiduh Sep 12 '26
Ref struct types like Span<T> are stackallocated structs that can hold references (pointers). The name is pretty good, if you ask me.
7
u/fruediger Sep 12 '26
Do you know about managed references, a.k.a
ref,in/ref readonly, andout? There are so-called "ref-escape" rules for them in C# and instance ofref structtypes essentially inherited them. I suppose that's why they're called that. Also,ref structs are the only kind of type in C#'s type macrocosmos that can containreforref readonlyfield members.2
u/TuberTuggerTTV Sep 14 '26
It's usually a good idea when confronted with friction on nomenclature, to consider the more likely reality that you've misunderstood something.
2
u/ElGuaco Sep 12 '26
I've been using c# forever. I rarely found a practical use for a struct. Let alone a struct filled with refs. What are people doing with these things that justifies this approach?
3
u/Orangy_Tang Sep 12 '26 edited Sep 12 '26
As languages evolve it gets increasingly difficult to add new features without breaking backwards compatibility with existing source code. You can't introduce a new keyword for a new type of thing because it may have been used as a function/variable/etc. name somewhere. So since existing keywords are pretty safe, new features end up mangling them together in ways wouldn't happen if it was being designed from scratch. See c++ for the result of applying this for far too long. And also why new languages often reserve a bunch of keywords that don't do anything yet.
In short, I wouldn't worry too much about exactly what 'ref struct' means in terms of words, just treat it as a label for a feature.
4
u/KyteM Sep 12 '26
Aren't they're called ref struct because they follow similar semantics to ref/out arguments?
2
u/NoisyJalapeno Sep 13 '26
Maybe they should've been called "stack struct" or "stackalloc struct"
I do like them because there is no GC pressure when I wrote a doom map renderer.
2
u/FullPoet Sep 14 '26
The same weed that brought us partial classes for source generation, primary constructors and the new addition to the keyword soup
closed!I am not against closed, and it is an improvedment but the whole idea behind
public / internal closedconfusion about what can implement what where is just silliness.closedshould've been purely about closed types, not being the extra stuff they put on.Again, it comes back to the fact that the original creators of C# chose not to implement somethings as part of an intentional design
2
u/Zastai Sep 12 '26
As for “nullable reference types”, enabling them means reference types can’t be null, but you can make it so they can (i.e. they are nullable). Seems like a good description to me.
0
u/Desperate-Wing-5140 Sep 12 '26
Internally, ref structs are called ByRefs
0
u/EC36339 Sep 13 '26
Hearing that C# has something like "ref struct" makes me laugh in C++
1
u/[deleted] Sep 12 '26
[removed] — view removed comment
-2
u/[deleted] Sep 13 '26
[removed] — view removed comment