C#Elvis操作员-是否会评估参数?
快速提问 即使SomeObject.SomeDelegateProperty为null且不会发生调用,是否也会计算someOtherDelegate?我假设没有,因为(?)的点是在null时停止执行,但我需要确认C#Elvis操作员-是否会评估参数?,c#,operators,C#,Operators,快速提问 即使SomeObject.SomeDelegateProperty为null且不会发生调用,是否也会计算someOtherDelegate?我假设没有,因为(?)的点是在null时停止执行,但我需要确认 SomeObject.SomeDelegateProperty?.Invoke(someOtherDelegate()); 谢谢当SomeObject.SomeDelegateProperty==null时,它将停止执行。 正如Jeroen van Langen评论她我的测试示例:
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;
}
}
内部类程序
{
私有静态void Main(字符串[]args)
{
var SomeObject=new FakeClass();
SomeObject.SomeDelegateProperty?.Invoke(someOtherDelegate());
}
公共静态字符串someOtherDelegate()
{
返回字符串。空;
}
公开课假课
{
公共行动和财产;
}
}
和IL代码:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Размер кода: 31 (0x1f)
.maxstack 2
.locals init ([0] class StackOverflow.Examples.Program/FakeClass SomeObject)
IL_0000: nop
IL_0001: newobj instance void
StackOverflow.Examples.Program/FakeClass::.ctor()
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: ldfld class [mscorlib]System.Action`1<string>
StackOverflow.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 StackOverflow.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
.method private隐藏静态void Main(字符串[]args)cil托管
{
.入口点
//аззааазаааааааа
.maxstack 2
.locals init([0]类StackOverflow.Examples.Program/FakeClass SomeObject)
IL_0000:没有
IL_0001:newobj实例无效
StackOverflow.Examples.Program/FakeClass::.ctor()
IL_0006:stloc.0
IL_0007:ldloc.0
IL_0008:ldfld类[mscorlib]系统。操作'1
StackOverflow.Examples.Program/FakeClass::SomeDelegateProperty
IL_000d:dup
IL_000e:brtrue.s IL_0013
IL_0010:流行音乐
IL_0011:br.s IL_001e
IL_0013:调用字符串StackOverflow.Examples.Program::someOtherDelegate()
IL_0018:callvirt实例无效类
[mscorlib]系统.操作'1::调用(!0)
IL_001d:没有
IL_001e:ret
}//方法程序结束::Main
如何查看指令
IL_000e:brtrue.s IL_0013
检查SomeDelegateProperty
,如果它不为null,则调用地址IL_0013
上的someOtherDelegate
上的SomeDelegateProperty==null
上的SomeDelegateProperty
。尽管我回答说,你的答案通过证明/解释更加精确,因此我将其标记为答案-谢谢你的研究