VB.NET 空合并运算符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6792729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET null coalescing operator?
提问by RiddlerDev
Possible Duplicates:
Coalesce operator and Conditional operator in VB.NET
Is there a VB.NET equivalent for C#'s ?? operator?
Is there a built-in VB.NET equivalent to the C# null coalescing operator?
是否有与 C# 空合并运算符等效的内置 VB.NET?
回答by Cody Gray
Yes, there is, a long as you're using VB 9 or later (included with Visual Studio 2008).
是的,只要您使用的是 VB 9 或更高版本(包含在 Visual Studio 2008 中)。
You can use the version of the If
operatoroverloaded to accept only two arguments:
您可以使用重载的If
运算符版本来仅接受两个参数:
Dim myVar? As Integer = Nothing
Console.WriteLine(If(myVar, 7))
More information can be found herein a blog post by the VB.NET team.
更多信息可以发现这里在用VB.NET团队博客文章。
(Yes, this is an operator, even though it looks like a function. It will compile down to the same IL as the "proper" null-coalescing operator in C#.)
(是的,这是一个operator,即使它看起来像一个函数。它会编译成与 C# 中“正确的”空合并运算符相同的 IL。)
Example
例子
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.
回答by davecoulter
I don't believe that there is a built in VB.Net equivalent, but here's an answer: null coalesce operator in VB.Net(8)
我不相信有内置的 VB.Net 等效项,但这里有一个答案:VB.Net(8) 中的 null 合并运算符
回答by user541686
No. Use GetValueOrDefault
; that's why it's there!
号 使用GetValueOrDefault
; 这就是为什么它在那里!