VB.Net - 杂项运算符
VB.Net 支持其他一些重要的运算符。
| 运算符 | 描述 | 示例 |
|---|---|---|
| AddressOf | 返回过程的地址。 |
AddHandler Button1.Click, AddressOf Button1_Click |
| Await | 它应用于异步方法或 lambda 表达式中的操作数,以暂停方法的执行,直到等待的任务完成。 |
Dim result As res = Await AsyncMethodThatReturnsResult() Await AsyncMethod() |
| GetType | 它返回指定类型的 Type 对象。 Type 对象提供有关类型的信息,例如其属性、方法和事件。 |
MsgBox(GetType(Integer).ToString()) |
| Function 表达方式 | 它声明定义函数 lambda 表达式的参数和代码。 |
Dim add5 = Function(num As Integer) num + 5 'prints 10 Console.WriteLine(add5(5)) |
| If | 它使用短路求值有条件地返回两个值之一。 If 运算符可以使用三个参数或两个参数来调用。 |
Dim num = 5 Console.WriteLine(If(num >= 0, "Positive", "Negative")) |
示例
以下示例演示了其中一些运算符 −
Module assignment
Sub Main()
Dim a As Integer = 21
Console.WriteLine(GetType(Integer).ToString())
Console.WriteLine(GetType(Double).ToString())
Console.WriteLine(GetType(String).ToString())
Dim multiplywith5 = Function(num As Integer) num * 5
Console.WriteLine(multiplywith5(5))
Console.WriteLine(If(a >= 0, "Positive", "Negative"))
Console.ReadLine()
End Sub
End Module
当上面的代码被编译并执行时,会产生以下结果 −
System.Int32 System.Double System.String 25 Positive

