“??”有什么用?
可能的重复:
什么是“??”运算符?
请解释一下“??”的用途是什么在下面的代码中,什么是“??”用于.
if ((this.OrderDate ?? DateTime.MinValue) > DateTime.Today)
{ e.Description =“订单日期不得为将来的日期。”; 返回假; 谢谢
上面的代码位于 http://nettiers.com/EntityLayer.ashx
。
Possible Duplicate:
What is the “??” operator for?
Please explain me what is use of "??" in below code and what is "??" used for.
if ((this.OrderDate ?? DateTime.MinValue) > DateTime.Today)
{
e.Description = "The Order Date must not be in the future.";
return false;
}
the above code is at http://nettiers.com/EntityLayer.ashx
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(这是重复的,但很难搜索,因此我很高兴为将来的搜索提供更多另一个目标...)
这是 空合并运算符。本质上,它评估第一个操作数,如果结果为 null(空引用或可为空值类型的空值),则它评估第二个操作数。结果是最后有效评估的操作数。
请注意,由于其结合性,您可以编写:
if
E1
、E2
、E3
和E4
都是表达式类型为int?
- 将从E1
开始并继续直到找到非空值。第一个操作数必须是可为 null 的类型,但第二个操作数可以为不可为 null 的类型,在这种情况下,整个表达式类型不可为 null。例如,假设 E4 是
int
类型的表达式(但其余所有仍然是int?
),那么您可以使x
不可为空:(This is a duplicate, but it's hard to search for, so I'm happy enough to provide more another target for future searches...)
It's the null-coalescing operator. Essentially it evaluates the first operand, and if the result is null (either a null reference or the null value for a nullable value type) then it evaluates the second operand. The result is whichever operand was evaluated last, effectively.
Note that due to its associativity, you can write:
if
E1
,E2
,E3
andE4
are all expressions of typeint?
- it will start withE1
and progress until it finds a non-null value.The first operand has to be a nullable type, but e second operand can be non-nullable, in which case the overall expression type is non-nullable. For example, suppose E4 is an expression of type
int
(but all the rest are stillint?
then you can makex
non-nullable: