“??”是什么? 运算符为?
I was wondering about ??
signs in C#
code. What is it for? And how can I use it?
What about int?
? Is it a nullable int?
See also:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
它是空合并运算符。 它是在 C# 2 中引入的。
表达式
a ?? 的结果 如果 b
不为 null,则为a
,否则为b
。 除非需要,否则不会评估b
。两件好事:
表达式的整体类型是第二个操作数的类型,这在您使用可空值类型时很重要:
<前><代码>int? 也许= ...;
int 肯定 = 也许? 10;
(请注意,您不能使用不可为 null 的值类型作为第一个操作数 - 它将是
毫无意义。)
关联性规则意味着您可以非常轻松地链接它。 例如:
这将使用送货地址、帐单地址或联系地址中的第一个非空值。
It's the null coalescing operator. It was introduced in C# 2.
The result of the expression
a ?? b
isa
if that's not null, orb
otherwise.b
isn't evaluated unless it's needed.Two nice things:
The overall type of the expression is that of the second operand, which is important when you're using nullable value types:
(Note that you can't use a non-nullable value type as the first operand - it would be
pointless.)
The associativity rules mean you can chain this really easily. For example:
That will use the first non-null value out of the shipping, billing or contact address.
它被称为“空合并运算符”,其工作原理如下:
您
现在可以执行以下操作:
It's called the "null coalescing operator" and works something like this:
Instead of doing:
You can now just do:
这就是合并运算符。 它本质上是以下
关于运算符
That is the coalesce operator. It essentially is shorthand for the following
MSDN Documentation on the operator
这是新的 Null Coalesce 运算符。
这 ?? 如果左侧操作数不为空,则运算符返回左侧操作数,否则返回右侧操作数。
你可以在这里读到它:
http://msdn.microsoft.com/en-us/库/ms173224(VS.80).aspx
It's the new Null Coalesce operator.
The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.
You can read about it here:
http://msdn.microsoft.com/en-us/library/ms173224(VS.80).aspx
它是以下功能的快捷方式:
It is a shortcut for:
它是合并运算符。 如果第一个值为 null,它将返回另一个值
it's the coalesce operator. it will return another value if the first value is null
它检查类别是否为空 - 如果是这种情况,空值将被“Home”替换。
it checks if category is null - when this is the case the null value is replaced by "Home".
我最喜欢的空合并运算符的用途之一是避免代码中的 if 语句(我认为 if 语句很丑陋,而且大多数时候只会让事情变得混乱)。 例如,采用一种典型的场景,如果可用,人们可能会选择从缓存加载某些内容,否则从数据库加载并填充缓存。
对我来说,这是丑陋的代码。 我可能有点肛门,但为什么不将其重构为这个呢?
在我看来,它更紧密地遵循 SRP,并且更清晰、更易于阅读。 这些功能各自执行一个明确可识别的功能。
One of my favorite uses for the null coalescing operator is to avoid if statements in my code (I think if statements are ugly and just clutter things up most times). For example, take a typical scenario where one might choose to load something from cache if available, otherwise load from the db and populate the cache.
To me, that's ugly code. I may be a bit anal, but why not refactor it to this instead?
It more closely follows SRP and is cleaner and easier to read, IMO. The functions perform exactly one clearly identifiable function each.
如果类别为空,文本将变为“Home”
if category is null, Text will become "Home"
返回第一个非空值。 便利。
Returns the first not-null value. Handy.
?? 空合并运算符
int? 是一个可空 int,这意味着它可以具有普通 int 和 null 的值。
有关详细信息,请阅读此。
?? Null-Coalescing Operator
int? is a nullable int, which means it can have the values of a normal int and null.
Read this for details.
这就是空合并运算符。 它与 可空类型(除其他外)一起使用, 对不起 :)
That's the null-coalescing operator . It's used with nullable types (among other things, sorry :)