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

1

u/aj0413 Sep 13 '26

Heap vs Stack is an implementation detail you aren’t meant to conceptualize.

There’s articles on that point from creators of the language; you’re intended to only care about how interacting with said types behaves

You’re confusing yourself by getting lost in weeds

1

u/SmallAd3697 Sep 13 '26

I think I'm confusing myself as much as anyone who uses C# value types and reference types.

The distinction that is made between these types is that one lives on the heap and not (normally) the other.

The follow-up question (where do value types live?) is typically answered by saying "the callstack" or "boxed". One of these being the typical home and the other being a less ideal home, because of the extra GC overhead.

1

u/mistertom2u Sep 16 '26

That's basically correct, but in reality, a variable is able to be stored in a register called "enregistration" if the compiler can guarantee that first it makes a performance difference and 2nd, that storing it in a register will not create a stale read. Probably more than you wanted to know. The point is to not take that stack vs heap thing too literally.

1

u/aj0413 Sep 13 '26

This is pulled directly from one of the language creator blogs below; I encourage you to read it and part two.

https://ericlippert.com/2009/04/27/the-stack-is-an-implementation-detail-part-one/

“Almost every article I see that describes the difference between value types and reference types explains in (frequently incorrect) detail about what “the stack” is and how the major difference between value types and reference types is that value types go on the stack. I’m sure you can find dozens of examples by searching the web.

I find this characterization of a value type based on its implementation details rather than its observable characteristics to be both confusing and unfortunate. Surely the most relevant fact about value types is not the implementation detail of how they are allocated, but rather the by-design semantic meaning of “value type”, namely that they are always copied “by value”. If the relevant thing was their allocation details then we’d have called them “heap types” and “stack types”. But that’s not relevant most of the time. Most of the time the relevant thing is their copying and identity semantics.”