Cod*_*ray 94
是的,只要您使用的是VB 9或更高版本(包含在Visual Studio 2008中).
您可以使用重载If运算符的版本来仅接受两个参数:
Dim myVar? As Integer = Nothing
Console.WriteLine(If(myVar, 7))
Run Code Online (Sandbox Code Playgroud)
更多信息可以发现这里在用VB.NET团队博客文章.
(是的,这是一个运算符,即使它看起来像一个函数.它将编译为与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.
Run Code Online (Sandbox Code Playgroud)
Meh*_*dad -3
编号 使用GetValueOrDefault; 这就是它存在的原因!
| 归档时间: |
|
| 查看次数: |
30183 次 |
| 最近记录: |