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())
函数表达式 - 它声明了定义函数
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"))
示例
以下示例演示了一些这些运算符,文件:misc_operators.vb -
Module misc_operators
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
执行上面示例代码,得到以下结果 -
F:\worksp\vb.net\operators>vbc misc_operators.vb
F:\worksp\vb.net\operators>misc_operators.exe
System.Int32
System.Double
System.String
25
Positive
上一篇:
VB.Net运算符
下一篇:
VB.Net决策结构