首页 > 其他 > 详细

null合并运算符【??】和null条件运算符【?.】

时间:2019-11-14 14:35:14      阅读:102      评论:0      收藏:0      [点我收藏+]
  • null合并运算符【??】

赋值的结果中的变量如果为空则用??后面的值替代前面的变量,否则直接用前面的变量

如果此运算符的左操作数不为 null,则此运算符将返回左操作数;否则返回右操作数

如:

var result = a;
if(a == null)
{
result = "";
}
//以上代码等同于 result = a
?? "";
  • null条件运算符【?.】

用于在执行成员访问 (?.) 或索引 (?[) 操作之前,测试是否存在 NULL。 这些运算符可帮助编写更少的代码来处理 null 检查,尤其是对于下降到数据结构。

如:

int? count = customers?[0]?.Orders?.Count(); 
 // null if customers, the first customer, or Orders is null

 

参考:原文链接:https://blog.csdn.net/w200221626/article/details/50779564

 

null合并运算符【??】和null条件运算符【?.】

原文:https://www.cnblogs.com/maooyt/p/11856507.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!