+ 4
C# method confusion...
Consider the following code snippet: static void Main(String[] arg) { Console.WriteLine(Kay()[1]); } static int[] Kay() { int[] x = {55, 2}; return x; } In my opinion, 'x', is destroyed and the method 'Kay()' returns the reference to the integer array object. If this is the case, then why does the output show the correct result, from the reference to the object which was cleared (or was it?) after exiting the method?
20 ответов
+ 3
Calvin Thomas You're correct in saying:
----
"`x` is destroyed and the method `Kay()` returns the reference to the integer array object."
--
However, it's only a reference on the current call stack that's destroyed, not the actual object. The actual object remains on the managed heap until all references have been cleared.
In this case, the object is cleared for garbage collection after the reference to Key()[1] is passed into Console.WriteLine in the Main method.
+ 4
Calvin Thomas - You wrote:
----
"I guess thar every method returning a reference will make an extra variable for storing the reference."
--
Not quite. It might be a little confusing without understanding how assembly-like instructions and compiler optimizations work. Truth be told... I barely have enough exposure to these myself. 😉
Essentially, the return value isn't stored in any variable in this example code.
It's about as fleeting as the integer value passed into the indexer: `Kay()[1]`.
+ 3
Here... the following might be happening:
1. Call Kay() and push reference to the returned int array onto the stack.
2. Push integer 1 onto the stack.
3. Pop the int array and pop the int value 1 off the stack to load and push the 2nd indexed value onto the stack.
At this point, the returned int array and the int value 1 have been popped from the stack and only the 2nd value from the array is on the stack.
4. Pop the value on the stack and use it as the arg for calling Console.WriteLine.
Now all values are cleared from the stack and the method terminates.
Hopefully, this helps illustrate what's happening.
+ 2
Lily Mea Is Calvin Thomas was telling about the parameters which are passed to the user-defined functions?
+ 1
As you are passing it with 1 it is showing 2.
+ 1
After completing the task of the function or returning the value X's work is over
+ 1
At print statement
x[1] is going as you are returning the whole array
+ 1
Calvin Thomas the address of the array object
+ 1
Calvin Thomas yeah..x is destroyed and the function return just the value of the x(rvalue)
+ 1
Key is a static method,
static stuff is not destroyed when abandone method scope like regular local vars, but x arr is not explicitly static so now you're making me doubt.
Anyways, the object will not be destroyed by the GC if there still an active reference in the current callstack, in that case, in the end lifecycle of Main()
+ 1
Calvin Thomas The return value is pushed onto the execution stack by `Kay()` and then popped off immediately when called by the indexer `[1]`, which pushes an int value onto the stack. That int value is then popped off the stack by the call to WriteLine(). Then poof... it's splitsville. 🤣
+ 1
David Carroll Now I understand. Thanks!
0
Lily Mea So is an additional copy of the array created for the new address to work? 'x' is destroyed after the function exits, right?
0
Atul I was just wondering what address the function returns, as 'x' is local to the Kay method and the address dereferenced in the Main method contains the previous data.
0
Atul So whose address is returned by the method?
0
I see, thanks.
0
David Carroll Thank you for the explanation.
0
David Carroll But where is this return value stored in the stack? When is it cleared?
0
Kiwwi# and David Carroll Thank you! @Kiwwi# I guess thar every method returning a reference will make an extra variable for storing the reference.
0
When one understand the scope & life-cycle of code entities, can get the true utility of lambdas (in-line).