是否有一个内建VB.NET等效于C#空合并运算符?
发布于 2011-07-22 16:06:23
是的,只要您使用的是VB 9或更高版本(包括在Visual 2008中),就可以使用。
您可以使用重载的运算符版本只接受两个参数:
Dim myVar? As Integer = Nothing
Console.WriteLine(If(myVar, 7))更多信息可以在这里团队的博客文章中找到。
(是的,这是一个操作符,尽管它看起来像一个函数。它将编译成与C#中的“适当”空合并操作符相同的IL。
示例
Dim b As Boolean?
Console.WriteLine("{0}.", If(b, "this is expected when b is nothing"))
'output: this is expected when b is nothing.
b = False
Console.WriteLine("{0}.", If(b, "this is unexpected when b is false"))
'output: False.
b = True
Console.WriteLine("{0}.", If(b, "this is unexpected when b is true"))
'output: True.发布于 2011-07-22 16:04:03
根据这个问题,答案似乎是如果()
发布于 2011-07-22 16:03:39
不是的。使用GetValueOrDefault,这就是它存在的原因!
https://stackoverflow.com/questions/6792729
复制相似问题