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)
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.
The return statement ends function execution and specifies a value to be returned to the function caller.
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.
Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With