❔ Can someone help me fully understand ref returns?
I have not been able to get this through my head aside from a few things like never using a ref return if a variable does not exist outside of scope. What about something like keeping local variables alive through ref returns?
Seeing the Span<T> type do these things confuses me. How is it able to do something like this considering ref return constraints, knowing it's a ref struct only on the stack? It's local but the GC knows it's still in use after returning
But you can't do anything like this which is what confuses me about the whole thing:
7 Replies
local variables gets deleted when the program exits the method
so, that span example isn't getting a ref to the array local
it's getting a ref to the first item of the array, which is on the heap
which has a lifetime longer than your method
so it's fine to return
You can do the second thing if you pass that n as ref
is there a way, a tool, to see what happens to variables and other stuffs during the debug, or you have just know it by theory?
Debug > Windows > Locals
debugger
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.