问题描述
在C#中,有一个空条件运算符?.
(有时称为Elvis运算符),如下所示:
cIn C# there is a null conditional operator ?.
(sometimes called the Elvis operator) like so:
var name = project?.customer?.name;
这不会失败,但是如果 project
或 customer
为null,则返回 null
.
which doesn't fail but instead return null
if project
or customer
is null.
VB.NET中是否有等效项?
Is there an equivalent in VB.NET?
请注意,我不是要查找 If(b,x,y)
,但是非常 ?.
替换.
Note that I am not looking for If(b, x, y)
but the very ?.
replacement.
推荐答案
VB还具有空条件运算符(从未听说过猫王"运算符):
VB also has the null conditional operator (never heard the term 'Elvis' operator):
Dim name = customer?.name
注意:
-
在VB中推断键入要求"Option Infer On"(选择推断开启)
Inferred typing in VB requires 'Option Infer On'
我非常确定您原来的C#代码示例应该是:var name = customer?.name;
I'm pretty sure that your original C# code sample should have been: var name = customer?.name;
这篇关于Vbnet中是否有空条件运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!