An expression is built from literals, names, calls and operators. Visual Basic uses words as well as symbols for operators, and the precedence of some of them differs from C-like languages: ^ binds tighter than unary minus, \ and Mod have their own levels, and & binds looser than +.
Precedence
From the highest to the lowest (the specification’s table; every binary operator is left-associative):
| Level | Operators |
|---|---|
| primary | every expression that is not an operator expression |
| await | Await |
| exponentiation | ^ |
| unary | + - (sign) |
| multiplicative | * / |
| integer division | \ |
| modulus | Mod |
| additive | + - |
| concatenation | & |
| shift | << >> |
| relational | = <> < > <= >= Like Is IsNot (and TypeOf … Is) |
| logical NOT | Not |
| logical AND | And AndAlso |
| logical OR | Or OrElse |
| logical XOR | Xor |
The operators
Dim a = 7 \ 2 ' integer division: 3
Dim b = 7 / 2 ' floating division: 3.5
Dim c = 7 Mod 3 ' 1
Dim d = 2 ^ 10 ' 1024.0 (Double)
Dim e = "a" & "b" & 1 ' concatenation
Dim f = 1 << 3 ' shift: 8
Dim g = 256 >> 2 ' 64
Dim h = Not True
Dim i = True And False
Dim j = True AndAlso False
Dim k = True Or False
Dim l = True OrElse False
Dim m = True Xor True
Dim n = "apple" Like "a*"
Dim o = "x" <> "y"
Dim x As Object = "s"
Dim p = x Is Nothing
Dim q = x IsNot Nothing
Dim r = TypeOf x Is String
Dim s = TypeOf x IsNot Integer
| Operator | Meaning |
|---|---|
+ - * | add, subtract, multiply (the result type follows the operand types; unary - negates). + on two strings concatenates, but prefer & |
/ | floating-point division (7 / 2 is 3.5; integers are converted to Double) |
\ | integer division, rounding toward zero |
Mod | the remainder; takes the sign of the left operand |
^ | exponentiation, always Double for the primitive numeric types |
& | string concatenation; operands are converted to strings |
<< >> | shift of an integral value (the count is masked to the width of the type) |
= <> < > <= >= | comparison, giving a Boolean; = is also the assignment symbol, and the position says which. String comparison follows Option Compare |
Like | pattern match on a string: ? one character, * any number, # one digit, [abc] or [a-c] one of a set, [!abc] one not in it |
Is, IsNot | reference comparison (x Is Nothing); IsNot is the negation (version 8) |
TypeOf x Is T, TypeOf x IsNot T | type test (IsNot since version 14) |
And Or Xor Not | logical on Boolean, bitwise on integers; both operands are always evaluated |
AndAlso OrElse | short-circuit: the right operand is evaluated only if needed |
Assignment
x = expression assigns; there is no assignment expression (an = inside an expression is a comparison). Compound forms apply an operator and assign: += -= *= /= \= ^= &= <<= >>=. Mid(text, start, length) = "xy" overwrites part of a string. Arguments can be named with := (Foo(count:=3)).
Dim total As Integer = 0
total += 5 : total -= 1 : total *= 3 : total \= 2
Dim txt = "x"
txt &= "y"
total <<= 1
total >>= 1
^= works on a Double.Other expression forms
Dim t = If(a > 1, "big", "small") ' conditional: only one branch is evaluated
Dim u = If(name, "default") ' coalescing: name if not Nothing
Dim v = node?.Name ' null-conditional (version 14)
Dim w = node?.Next1?.Name
Dim lookup = dict!one ' dictionary access: dict("one")
Dim size = GetType(Integer).Name
Dim nm = NameOf(total) ' "total"
Dim list = New List(Of Integer)() From {1, 2, 3} ' collection initialiser
Dim p = New With {.Id = 1, .Name = "x"} ' anonymous type
Dim k = New With {Key .Id = 1} ' Key properties form equality
Dim n = New Node With {.Name = "n"} ' object initialiser
Dim f As Func(Of Integer, Integer) = AddressOf Twice ' delegate to a method
If(a, b, c)andIf(a, b)evaluate lazily; the older functionIIfevaluates both. A nullableIf(x, 0)unwraps.?.and?(stop atNothingand giveNothing(a nullable for a value type); they also work for indexing,a?(0).- Member access:
x.Member; insideWith x … End Witha leading.Membermeansx.Member;Meis the current object,MyBasethe base class,MyClassthe current class bypassing overrides;x!Nameisx.Item("Name");x.<a>,x.@aandx...<a>are XML member access. - Calls:
Foo(1, 2); a method with no parameters may be called without brackets as a statement, and a call may use named arguments (Foo(b:=2, a:=1)); a bracket after an expression selects an index of an array or a default property.Call Foo(1)is the old statement form. - Creation:
New T(args)with an optionalWith {…}(object initialiser) orFrom {…}(collection initialiser), arraysNew Integer() {1, 2}andNew T(2) {…}, anonymous typesNew With {…}and delegate creationNew Func(Of Integer)(AddressOf M). - Type expressions:
GetType(T)gives aSystem.Type,NameOf(x)the name as a string,GetXmlNamespace(p)an XML namespace, andAddressOf Ma delegate to a method.Awaitwaits for a task (see procedures). - Lambdas and queries have their own pages: lambdas and LINQ.
A constant expression (a literal, a Const, or operators on them) is evaluated by the compiler. On Object, with Option Strict Off, member access and operators are late bound: the operation is resolved at run time (this is also how COM objects are used).
Not described
Known, not yet covered
- Reference: the result type of each arithmetic and comparison operator for each pair of primitive types, overflow behaviour, operator resolution and the rules for user-defined operators are in the operator chapters of the specification and are not tabulated here.
- Reference: overload resolution, extension method lookup, default instances of forms and the rules for late binding are only mentioned.
See also
- Statements and control flow – how expressions are used
- Lexical structure – the operator tokens