C# method confusion... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

4th Jun 2021, 9:57 AM
Calvin Thomas
Calvin Thomas - avatar
20 Answers
+ 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.
1st Jul 2021, 5:09 AM
David Carroll
David Carroll - avatar
+ 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]`.
2nd Jul 2021, 6:47 AM
David Carroll
David Carroll - avatar
+ 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.
2nd Jul 2021, 6:48 AM
David Carroll
David Carroll - avatar
+ 2
Lily Mea Is Calvin Thomas was telling about the parameters which are passed to the user-defined functions?
4th Jun 2021, 10:54 AM
Atul [Inactive]
+ 1
As you are passing it with 1 it is showing 2.
4th Jun 2021, 10:50 AM
Atul [Inactive]
+ 1
After completing the task of the function or returning the value X's work is over
4th Jun 2021, 4:52 PM
Atul [Inactive]
+ 1
At print statement x[1] is going as you are returning the whole array
4th Jun 2021, 5:01 PM
Atul [Inactive]
+ 1
Calvin Thomas the address of the array object
4th Jun 2021, 11:45 PM
durian
durian - avatar
+ 1
Calvin Thomas yeah..x is destroyed and the function return just the value of the x(rvalue)
4th Jun 2021, 11:52 PM
durian
durian - avatar
+ 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()
1st Jul 2021, 7:53 PM
Kiwwi#
Kiwwi# - avatar
+ 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. 🤣
1st Jul 2021, 11:27 PM
David Carroll
David Carroll - avatar
+ 1
David Carroll Now I understand. Thanks!
3rd Jul 2021, 1:34 AM
Calvin Thomas
Calvin Thomas - avatar
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?
4th Jun 2021, 4:39 PM
Calvin Thomas
Calvin Thomas - avatar
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.
4th Jun 2021, 4:41 PM
Calvin Thomas
Calvin Thomas - avatar
0
Atul So whose address is returned by the method?
4th Jun 2021, 4:55 PM
Calvin Thomas
Calvin Thomas - avatar
0
I see, thanks.
5th Jun 2021, 12:17 PM
Calvin Thomas
Calvin Thomas - avatar
0
David Carroll Thank you for the explanation.
1st Jul 2021, 6:46 AM
Calvin Thomas
Calvin Thomas - avatar
0
David Carroll But where is this return value stored in the stack? When is it cleared?
1st Jul 2021, 6:56 AM
Calvin Thomas
Calvin Thomas - avatar
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.
2nd Jul 2021, 2:42 AM
Calvin Thomas
Calvin Thomas - avatar
0
When one understand the scope & life-cycle of code entities, can get the true utility of lambdas (in-line).
2nd Jul 2021, 7:22 AM
Kiwwi#
Kiwwi# - avatar