VB.Net - Miscellaneous Operators
Advertisements
There are few other important operators supported by VB.Net.
| Operator | Description | Example |
|---|---|---|
| AddressOf | Returns the address of a procedure. | AddHandler Button1.Click, AddressOf Button1_Click |
| Await | It is applied to an operand in an asynchronous method or lambda expression to suspend execution of the method until the awaited task completes. | Dim result As res = Await AsyncMethodThatReturnsResult() Await AsyncMethod() |
| GetType | It returns a Type object for the specified type. The Type object provides information about the type such as its properties, methods, and events. | MsgBox(GetType(Integer).ToString()) |
| Function Expression | It declares the parameters and code that define a function lambda expression. | Dim add5 = Function(num As Integer) num + 5 'prints 10 Console.WriteLine(add5(5)) |
| If | It uses short-circuit evaluation to conditionally return one of two values. The If operator can be called with three arguments or with two arguments. | Dim num = 5 Console.WriteLine(If(num >= 0, "Positive", "Negative")) |
Example
The following example demonstrates some of these operators:
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
When the above code is compiled and executed, it produces following result:
System.Int32 System.Double System.String 25 Positive