是否有 C# 的 VB.NET 等效项??? 操作员?
是否有与 C# 的 ??
运算符等效的 VB.NET?
Is there a VB.NET equivalent for C#'s ??
operator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是否有与 C# 的 ??
运算符等效的 VB.NET?
Is there a VB.NET equivalent for C#'s ??
operator?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
使用带有两个参数的
If()
运算符 (Microsoft 文档):Use the
If()
operator with two arguments (Microsoft documentation):IF()
运算符应该可以为您解决问题:http: //visualstudiomagazine.com/listings/list.aspx?id=252
The
IF()
operator should do the trick for you:http://visualstudiomagazine.com/listings/list.aspx?id=252
接受的答案没有任何解释,只是一个链接。
因此,我想我应该留下一个答案来解释
If
运算符如何工作,摘自 MSDN:If 运算符 (Visual Basic)
如果使用两个参数调用运算符
零件
VB
如何处理两个以上值(嵌套
if
)的示例:The accepted answer doesn't have any explanation whatsoever and is simply just a link.
Therefore, I thought I'd leave an answer that explains how the
If
operator works taken from MSDN:If Operator (Visual Basic)
If Operator Called with Two Arguments
Parts
VB
An example of how to handle more than two values (nested
if
s):您可以使用扩展方法。 这个方法的工作原理类似于 SQL
COALESCE
,对于您要测试的内容来说可能有些过大,但它确实有效。内置的
If(nullable, secondaryChoice)
只能处理两个可为空的选择。 在这里,我们可以根据需要合并
任意数量的参数。 第一个非空参数将被返回,之后其余参数不再被求值(短路,如AndAlso
/&&
和或者Else
/||
)You can use an extension method. This one works like SQL
COALESCE
and is probably overkill for what you are trying to test, but it works.The built-in
If(nullable, secondChoice)
can only handle two nullable choices. Here, one canCoalesce
as many parameters as desired. The first non-null one will be returned, and the rest of the parameters are not evaluated after that (short circuited, likeAndAlso
/&&
andOrElse
/||
)大多数这些解决方案的一个重要限制是它们不会短路。 因此,它们实际上并不等同于
??
。内置
If
运算符不会计算后续参数,除非前面的参数计算结果为空。以下语句是等效的:
C#
VB
这将在
??
工作的所有情况下工作。 任何其他解决方案都必须极其谨慎地使用,因为它们很容易引入运行时错误。The one significant limitation of most of these solutions is that they won't short-circuit. They are therefore not actually equivalent to
??
.The built-in
If
operator won't evaluate subsequent parameters unless the earlier parameter evaluates to nothing.The following statements are equivalent:
C#
VB
This will work in all cases where
??
works. Any of the other solutions would have to be used with extreme caution, as they could easily introduce run-time bugs.在此处查看有关 If 运算符 (Visual Basic) 的 Microsoft 文档:https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/if-operator
以下是一些示例 (VB.Net)
Check Microsoft documentation about If Operator (Visual Basic) here: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/if-operator
Here are some examples (VB.Net)