如果没有填充到预期类型的​​声明变量,那么来自例如方法调用的返回值会在哪里?

Bir*_*man 6 .net c# return-value

我们不会被迫将返回值从例如方法调用填充到预期类型的​​声明变量中,但在那种情况下会发生什么?

以下返回值在哪里/它会发生什么:?

decimal d = 5.5m;
Math.Round(d, MidpointRounding.AwayFromZero);
Run Code Online (Sandbox Code Playgroud)

显然,如果我想查看方法调用的结果,我会执行以下操作:

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

(这个问题不是特定于值类型,而是特定于引用类型)

SLa*_*aks 8

它从执行堆栈中弹出:

IL_000A:  call        System.Math.Round
IL_000F:  pop         
Run Code Online (Sandbox Code Playgroud)

如果它是引用类型,则引用将从堆栈中弹出,并且最终将由GC收集对象本身(假设它没有其他引用).