问题标签 [null-conditional-operator]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
4 回答
214 浏览

c# - ?. C# 中的运算符由于未知原因未编译

在以下代码中,两个变体之一无法编译:

错误 CS0023:操作员“?” 不能应用于“十进制”类型的操作数

为什么简单表单无法编译?

表达式c?.DecimalField不会是 typedecimal?吗?该值可以为 null,因此?.应应用运算符。我很确定这是因为在这段代码中:

var确实根据decimal?IDE解析。

0 投票
2 回答
438 浏览

c# - 当 null 条件运算符短路时,它是否仍然评估方法参数?

null 条件运算符可用于跳过对 null 目标的方法调用。在这种情况下是否会评估方法参数?

例如:

什么GetFromNetwork时候myObjectnull

0 投票
1 回答
8837 浏览

c# - 使用空条件布尔?在 if 语句中

为什么此代码有效:

但这段代码没有:

错误 CS0266 无法隐式转换类型“bool?” '布尔'

那么为什么在if语句中进行这种隐式转换不是语言特性呢?

0 投票
3 回答
2317 浏览

c# - Using the Null Conditional Operator to check values on objects which might be null

I've been playing with C# 6's Null Conditional Operator (more info here).

I really like the syntax and I think it makes the code much more readable however I think it is questionable as to what exactly the code is going to do when you come across checking the value of a property on an object which itself might be null.

For example, if I had a class with a decimal property on it and I wanted a conditional check on the value of that decimal, I would write something like:

On the surface this looks great... If foo is not null, get the value of Bar and check if it's greater than a maximum value, if it is, do something.

However, what if foo is null?!

This documentation about the new and improved features of C# 6 says something along these lines:

if in fact the value of the object is null, the null-conditional operator will return null. It short-circuits the call to Bar, and immediately returns null, avoiding the programming error that would otherwise result in a NullReferenceException.

I've written a fiddle here which shows that it does indeed work and is doing what I'm expecting it to do however I can't get my head around how it is deciding the result of the condition.

How does the short-circuit equal a false? In my head this code is now going to say "If foo is null, check if null is > max, which is impossible, so return false" or "If foo is null, then foo != null will return false, so you get a false" however the documentation says the null conditional check returns null, not false.

0 投票
1 回答
296 浏览

vb.net - 否定 null 条件运算符会返回意外结果

如果变量值为 Nothing,我们会遇到与 null 条件运算符有关的意外行为。

以下代码的行为让我们有些困惑

Not l?.Any()如果l没有条目或什么都没有,则预期的行为是真实l的。但如果lis Nothing 结果是错误的。

这是我们用来查看实际行为的测试代码。

结果:

  • 没有什么是假的
  • 不是没有什么是真实的
  • 什么都没有?.Any() 是假的
  • Not Nothing?.Any() 是假的

如果评估为真,为什么不是最后一个?

C# 完全阻止我编写这种检查......

0 投票
1 回答
66 浏览

php - 空条件运算符是否总是在第一次出现空时中断?

拥有 C# 背景,我在访问?.类属性时(使用实体框架关系时)总是使用运算符。

例如,如果我使用它并且状态类为空,它会抛出异常,因为我试图从空对象中获取属性。

为了解决这个问题,我可以这样做:

运算符将?.在第一个 null 处中断并跳过其余的访问。

我在 PHP 中遇到了同样的情况,不得不做这样的事情:

如果状态为空,它会抛出相同的异常(试图从空对象中获取属性)。

但后来我这样做了,它奏效了:

显然,->text如果status为空,PHP 将跳过访问。

创建了一个小提琴来检查这是否不仅在我的环境中,而且令人惊讶的是它工作得很好!

<<“你好!”

这真的是??PHP 中运算符的预期行为吗?因为我没有在文档中看到这一点。

0 投票
4 回答
7804 浏览

c# - 如果为空,则使用空条件运算符将值设置为 0

我是 C# 新手,但不是一般的编程新手。我正在尝试为我的程序添加一些错误检查。有 3 个文本框,我正在尝试制作它,以便如果文本框留空,则假定值为 0。这是我到目前为止的代码:

这段代码有效,但我确信我可以用更简单的东西替换那些 if 语句。我已经看到如何使用 null 条件运算符将一个变量设置为 null 如果另一个变量为 null,但我并没有真正掌握它以使其适应我的场景。

0 投票
2 回答
647 浏览

c# - 为什么我可以省略调用链中的后续空条件运算符?

考虑以下代码:

它分配nulltt。问题是:为什么它可以正常工作?

我想我必须?.在 Select as ?.Where(...)return之前使用null。此外,如果我将第二行分成两行:

ArgumentNullException在第三行会有yy == null.

有什么魔力?:)
如果这是因为短路,我从没想过它会这样。

0 投票
1 回答
48 浏览

c# - 为什么我们不需要使用字符串?str 并且对于小数或 int 我们需要使用小数?价格 = p?价格

我是 C# 的新手。我对空条件运算符感到困惑。

如果字符串每个人都使用这条线

但在十进制或浮点数的情况下

0 投票
3 回答
206 浏览

c# - 方法参数的 C# 空条件简写

当方法属于所讨论的对象时,空条件运算符非常有用,但如果所讨论的对象是参数怎么办?例如,这可以缩短吗?

我想到的一种解决方案是使用如下扩展方法:

这样,第一个代码块可以简化为:

但是此解决方案特定于此示例(即,如果对象不为空,则将对象添加到列表中)。有没有一种通用的方法来指示如果参数为空,则不应该运行该方法?