4 ms·
you can stack allocate with just `struct` in C#. if you put a struct in a local variable, it'll be on the stack `ref struct` allows that struct to contain refs
by harrison_clarke 2y ago
you can stack allocate with just `struct` in C#. if you put a struct in a local variable, it'll be on the stack
`ref struct` allows that struct to contain refs, and disallows putting it on the heap (so, you can't have an array/list of ref structs, and a class or a normal struct can't have a ref struct field)
- neonsunset 2y agoThis is correct. Ref structs and refs are also subject to similar[0] to Rust's lifetime analysis to prevent them from being ever invalidated and having use-after-free (even if that free is essentially going out of scope or popping a stack frame). I assume pjc50 specifically refers to the fact that `ref struct` gives a strong guarantee while a regular struct can be placed wherever. [0]: https://em-tg.github.io/csborrow/ https://em-tg.github.io/csborrow/
- harrison_clarke 2y agoi think of it like having rust's rules, but you only get one (implicit) lifetime variable (that's probably wrong in some subtle ways)