简体   繁体   English

在Vbnet中有一个空条件运算符吗?

[英]is there a null conditional operator in Vbnet?

cIn C# there is a null conditional operator ?. c在C#中有一个空条件运算符?. (sometimes called the Elvis operator) like so: (有时称为猫王操作员)如下:

var name = project?.customer?.name;

which doesn't fail but instead return null if project or customer is null. 这不会失败,而是返回null ,如果projectcustomer为空。

Is there an equivalent in VB.NET? VB.NET中有相同的东西吗?

Note that I am not looking for If(b, x, y) but the very ?. 请注意,我不是在寻找If(b, x, y)而是非常?. replacement. 替代。

VB also has the null conditional operator (never heard the term 'Elvis' operator): VB也有空条件运算符(从未听过术语'Elvis'运算符):

Dim name = customer?.name

Notes: 笔记:

  1. Inferred typing in VB requires 'Option Infer On' 在VB中推断输入需要'Option Infer On'

  2. I'm pretty sure that your original C# code sample should have been: var name = customer?.name; 我很确定你的原始C#代码示例应该是:var name = customer?.name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM