本文介绍了C#空传播 - 哪里魔术发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 空的传播是一个很不错的功能 - 但 ,其中的和 如何的不实际的魔术发生? 哪里 FRM .Close()如果(FRM!= NULL)得到改变,以 frm.Close(); - 它实际上得到改变那种代码在所有Null propagation is a very nice feature - but where and how does the actual magic happen?Where does frm?.Close() get changed to if(frm != null) frm.Close(); - Does it actually get changed to that kind of code at all?推荐答案这是由编译器完成?它不会改变 FRM .Close()到如果(FRM!= NULL)frm.Close(); 在重新编写源代码的条款,但它的确实的发射IL字节码检查空It is done by the compiler. It doesn't change frm?.Close() to if(frm != null) frm.Close(); in terms of re-writing the source code, but it does emit IL bytecode which checks for null.看看下面的例子:void Main(){ Person p = GetPerson(); p?.DoIt();} 编译为:Compiles to:IL_0000: ldarg.0IL_0001: call UserQuery.GetPersonIL_0006: dupIL_0007: brtrue.s IL_000BIL_0009: popIL_000A: retIL_000B: call UserQuery+Person.DoItIL_0010: ret这可以解读为: 呼叫 - 呼叫 GetPerson( ) - 存储在堆栈上的结果结果 DUP - 推值压入调用堆栈(再次) brtrue.s - 弹出堆栈顶部的值。如果这是真的,还是不空(引用类型),然后跳转到 IL_000Bcall - Call GetPerson() - store the result on the stack.dup - Push the value onto the call stack (again)brtrue.s - Pop the top value of the stack. If it is true, or not-null (reference type), then branch to IL_000B如果结果是假的(也就是说,对象是的空的)结果 弹出 - 弹出堆栈(清除栈,我们没有不再需要人)结果 RET 的值 - 返回If the result is false (that is, the object is null)pop - Pops the stack (clear out the stack, we no longer need the value of Person)ret - Returns如果值是true(也就是说,对象是的不为空的)结果 呼叫 - 呼叫 DOIT()在堆栈的最顶部的值(目前的结果 GetPerson ) RET - 返回If the value is true (that is, the object is not null)call - Call DoIt() on the top-most value of the stack (currently the result of GetPerson).ret - Returns手动空检查:Person p = GetPerson();if (p != null) p.DoIt();IL_0000: ldarg.0IL_0001: call UserQuery.GetPersonIL_0006: stloc.0 // pIL_0007: ldloc.0 // pIL_0008: brfalse.s IL_0010IL_000A: ldloc.0 // pIL_000B: callvirt UserQuery+Person.DoItIL_0010: ret请注意,上面是的不的一样?,但是。检查的有效结果是相同的。Note that the above is not the same as ?., however the effective outcome of the check is the same.没有空检查:void Main(){ Person p = GetPerson(); p.DoIt();}IL_0000: ldarg.0IL_0001: call UserQuery.GetPersonIL_0006: callvirt UserQuery+Person.DoItIL_000B: ret 这篇关于C#空传播 - 哪里魔术发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!