Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does the returned value from e.g. a method call go if not filled into a declared variable of expected type?

We are not forced to fill the returned value from e.g. a method call into a declared variable of expected type, but what happens to it in that situation?

Where does the following returned value go/What happens to it: ?

decimal d = 5.5m;
Math.Round(d, MidpointRounding.AwayFromZero);

Obviously, if I wanted to see the result from the method call I would do the following:

decimal d = 5.5m;
decimal d2 = Math.Round(d, MidpointRounding.AwayFromZero); // Returns 6 into 
                                                           // the variable "d2"

(This question is NOT specific to value types, but also reference types)

like image 557
Birdman Avatar asked Jan 05 '12 14:01

Birdman


People also ask

Where does the returned value go in Java?

It doesn't go anywhere. The value / reference is simply discarded. It is as if you assigned it to a local variable that immediately goes out of scope.

When using the return statement where is the value returned to?

The return statement ends function execution and specifies a value to be returned to the function caller.

What happens when a return statement in a method is reached?

A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.

What is the return type of a method that does not return any value?

Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so.


1 Answers

It gets popped from the execution stack:

IL_000A:  call        System.Math.Round
IL_000F:  pop         

If it's a reference type, the reference will be popped from the stack, and the object itself will eventually be collected by the GC (assuming that it has no other references).

like image 88
SLaks Avatar answered Sep 18 '22 21:09

SLaks