如果无效操作中的所有参数均为空,则结果是什么?
-
09-10-2019 - |
题
当此代码完成时,是什么结果 myObject
?
object myObject = "something";
object yourObject = null;
myObject = null ?? yourObject;
解决方案
MyObject将是无效的
这被翻译成 -
if (null == null)
myObject = yourObject;
else
myObject = null;
其他提示
联合操作员可以转化为:
x ?? y
x != null ? x : y
因此,您拥有的是:
myObject = null != null ? null : yourObject;
实际上,这是毫无意义的,因为null始终将是空的。
只是为了踢,这是一张小桌子:
A ?? B -> R --------------------- a ?? any -> a; where a is not-null null ?? b -> b; for any b null ?? null -> null; implied from previous
从那以后 ??
只是(惊喜!)右相关的infix操作员, x ?? y ?? z
--> x ?? (y ?? z)
. 。喜欢 &&
和 ||
, ??
也是一个短路操作。
...从 ?操作员(C#参考):
如果不是零,则它将返回左手操作数;否则,它将返回正确的操作数。
...来自C#3.0语言参考:
表格A的无效表达? B要求A为无效类型或参考类型。如果a是非无效的,则a的结果是? b是a;否则,结果是b。该操作仅在a为null时评估b。
不隶属于 StackOverflow