C# Elvis Operator - 参数会被评估吗?

C# Elvis Operator - will the parameters be evaluated?

快速提问 即使 SomeObject.SomeDelegateProperty 为 null 并且不会发生调用,也会评估 someOtherDelegate 吗?我假设没有,因为 (?.) 的要点是在 null 时停止执行,但我需要确认。

SomeObject.SomeDelegateProperty?.Invoke(someOtherDelegate());

谢谢

当SomeObject.SomeDelegateProperty == null时会停止执行。 正如 Jeroen van Langen 评论的那样

她是我的测试例子:

internal class Program
{
    private static void Main( string[] args )
    {
        var SomeObject = new FakeClass();
        SomeObject.SomeDelegateProperty?.Invoke( someOtherDelegate() );
    }

    public static string someOtherDelegate()
    {
        return String.Empty;
    }

    public class FakeClass
    {
        public Action<string> SomeDelegateProperty;
    }
}

和IL代码:

.method private hidebysig static void  Main(string[] args) cil managed
{
    .entrypoint
    // Размер кода:       31 (0x1f)
    .maxstack  2
    .locals init ([0] class Whosebug.Examples.Program/FakeClass SomeObject)
    IL_0000:  nop
    IL_0001:  newobj     instance void 
    Whosebug.Examples.Program/FakeClass::.ctor()
    IL_0006:  stloc.0
    IL_0007:  ldloc.0
    IL_0008:  ldfld      class [mscorlib]System.Action`1<string> 
    Whosebug.Examples.Program/FakeClass::SomeDelegateProperty
    IL_000d:  dup
    IL_000e:  brtrue.s   IL_0013
    IL_0010:  pop
    IL_0011:  br.s       IL_001e
    IL_0013:  call       string      Whosebug.Examples.Program::someOtherDelegate()
    IL_0018:  callvirt   instance void class 
    [mscorlib]System.Action`1<string>::Invoke(!0)
    IL_001d:  nop
    IL_001e:  ret
} // end of method Program::Main

如何查看说明 IL_000e: brtrue.s IL_0013 检查 SomeDelegateProperty 如果它不是 null,则在地址 IL_0013 上调用 someOtherDelegate 否则转到结束地址 IL_001e .