运算符是一个符号,通知编译器执行特定的数学或逻辑操作。 VB.Net丰富的内置运算符,并提供以下类型的常用运算符:
算术运算符
比较运算符
逻辑/位运算符
位移位运算符
赋值运算符
其他运算符
本教程将介绍最常用的运算符。
下表显示了VB.Net支持的所有算术运算符。 假设变量A保持2,变量B保持7,则:
运算符 | 描述 | 例 |
---|---|---|
^ | Raises one operand to the power of anothe 将一个操作数作为为另一个的幂 | B^A will give 49 |
+ | Adds two operand 添加两个操作数s | A + B will give 9 |
- | Subtracts second operand from the first 从第一个操作数中减去第二个操作数 | A - B will give -5 |
* | Multiplies both operands 将两个操作数相乘 | A * B will give 14 |
/ | Divides one operand by another and returns a floating point result 将一个操作数除以另一个操作数,并返回一个浮点结果 | B / A will give 3.5 |
\ | Divides one operand by another and returns an integer result 将一个操作数除以另一个操作数,并返回一个整数结果 | B \ A will give 3 |
MOD | Modulus Operator and remainder of after an integer division 模数运算符和整数除法后的余数 | B MOD A will give 1 |
下表显示了VB.Net支持的所有比较运算符。 假设变量A保持10,变量B保持20,则:
运算符 | 描述 | 例 |
---|---|---|
= | Checks if the values of two operands are equal or not; if yes, then condition becomes true. 检查两个操作数的值是否相等; 如果是,则条件变为真。 | (A = B)是不正确的。 |
<> | Checks if the values of two operands are equal or not; if values are not equal, then condition becomes true. 检查两个操作数的值是否相等; 如果值不相等,则条件为真。 | (A<>B)为真。 |
> | Checks if the value of left operand is greater than the value of right operand; if yes, then condition becomes true. 检查左操作数的值是否大于右操作数的值; 如果是,则条件变为真。 | (A> B)是不正确的。 |
< | Checks if the value of left operand is less than the value of right operand; if yes, then condition becomes true. 检查左操作数的值是否小于右操作数的值; 如果是,则条件变为真。 | (A <B)为真。 |
> = | Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then condition becomes true. 检查左操作数的值是否大于或等于右操作数的值; 如果是,则条件变为真。 | (A> = B)是不正确的。 |
<= | Checks if the value of left operand is less than or equal to the value of right operand; if yes, then condition becomes true. 检查左操作数的值是否小于或等于右操作数的值; 如果是,则条件变为真。 | (A <= B)为真。 |
下表显示了VB.Net支持的所有逻辑运算符。 假设变量A保持布尔值True,变量B保持布尔值False,则:
运算符 | 描述 | 例 |
---|---|---|
And | It is the logical as well as bitwise AND operator. If both the operands are true, then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions. 它是逻辑以及按位AND运算符。 如果两个操作数都为真,则条件为真。 此运算符不执行短路,即,它评估两个表达式。 | (A和B)为假。 |
Or | It is the logical as well as bitwise OR operator. If any of the two operands is true, then condition becomes true. This operator does not perform short-circuiting, i.e., it evaluates both the expressions. 它是逻辑以及按位或运算符。 如果两个操作数中的任何一个为真,则条件为真。 此运算符不执行短路,即,它评估两个表达式。 | (A或B)为真。 |
Not | It is the logical as well as bitwise NOT operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false. 它是逻辑以及按位非运算符。 用于反转其操作数的逻辑状态。 如果条件为真,则逻辑非运算符将为假。 | 没有(A和B)为真。 |
Xor | It is the logical as well as bitwise Logical Exclusive OR operator. It returns True if both expressions are True or both expressions are False; otherwise it returns False. This operator does not perform short-circuiting, it always evaluates both expressions and there is no short-circuiting counterpart of this operator. 它是逻辑以及按位逻辑异或运算符。 如果两个表达式都为True或两个表达式都为False,则返回True; 否则返回False。 该运算符不会执行短路,它总是评估这两个表达式,并且没有该运算符的短路对应。 | 异或B为真。 |
AndAlso | It is the logical AND operator. It works only on Boolean data. It performs short-circuiting. 它是逻辑 AND 运算符。它仅适用于布尔型数据。它执行短路。 | (A AndAlso运算B)为假。 |
OrElse | It is the logical OR operator. It works only on Boolean data. It performs short-circuiting. 它是逻辑或运算符。 它只适用于布尔数据。 它执行短路。 | (A OrElse运算B)为真。 |
IsFalse | It determines whether an expression is False. 它确定表达式是否为假。 | |
IsTrue | It determines whether an expression is True. 它确定表达式是否为真。 |
p | q | p&Q | p | q | p ^ Q |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
假设A = 60; 和B = 13; 现在的二进制格式,他们将如下:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A | B = 0011 1101
A ^ B = 0011 0001
〜A = 1100 0011
运算符 | 描述 | 示例 |
---|---|---|
And | Bitwise AND Operator copies a bit to the result if it exists in both operands. 如果两个操作数都存在,则按位AND运算符将一个位复制到结果。 | (A AND B) will give 12, which is 0000 1100 |
Or | Binary OR Operator copies a bit if it exists in either operand. 二进制OR运算符复制一个位,如果它存在于任一操作数。 | (A Or B) will give 61, which is 0011 1101 |
Xor | Binary XOR Operator copies the bit if it is set in one operand but not both. 二进制XOR运算符复制该位,如果它在一个操作数中设置,但不是两个操作数。 | (A Xor B) will give 49, which is 0011 0001 |
Not | Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. 二进制补码运算符是一元的,具有“翻转”位的效果。 | (Not A ) will give -61, which is 1100 0011 in 2's complement form due to a signed binary number. |
<< | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. 二进制左移位运算符。 左操作数值向左移动由右操作数指定的位数。 | A << 2 will give 240, which is 1111 0000 |
>> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. 二进制右移运算符。 左操作数值向右移动由右操作数指定的位数。 | A >> 2 will give 15, which is 0000 1111 |
运算符 | 描述 | 例 |
---|---|---|
= | Simple assignment operator, Assigns values from right side operands to left side operand 简单赋值操作符,将值从右侧操作数分配给左侧操作数 | C = A + B A + B将赋值为C |
+ = | Add AND assignment operator, It adds right operand to the left operand and assigns the result to left operand 添加AND赋值运算符,向左操作数添加右操作数,并将结果赋值给左操作数 | C + = A等于C = C + A |
- = | Subtract AND assignment operator, It subtracts right operand from the left operand and assigns the result to left operand 减法AND赋值运算符,它从左操作数中减去右操作数,并将结果赋值给左操作数 | Ç - = A等于C = C - A |
* = | Multiply AND assignment operator, It multiplies right operand with the left operand and assigns the result to left operand 乘法AND赋值运算符,它将右操作数与左操作数相乘,并将结果赋值给左操作数 | C * = A等于C = C * A |
/ = | Divide AND assignment operator, It divides left operand with the right operand and assigns the result to left operand (floating point division) 除法AND赋值运算符,它用右操作数划分左操作数,并将结果分配给左操作数(浮点除法) | C / = A等于C = C / A |
\ = | Divide AND assignment operator, It divides left operand with the right operand and assigns the result to left operand (Integer division) 除法AND赋值运算符,它用右操作数划分左操作数,并将结果分配给左操作数(整数除法) | ç = A等于C = C A |
^ = | Exponentiation and assignment operator. It raises the left operand to the power of the right operand and assigns the result to left operand. 指数和赋值运算符。 它将左操作数提升为右操作数的幂,并将结果分配给左操作数。 | C ^ = A等于C = C ^ A |
<< = | Left shift AND assignment operator 左移AND赋值运算符 | C语言的<< = 2是同C = C << 2 |
>> = | Right shift AND assignment operator 右移AND赋值运算符 | C >> = 2 >> 2同C = C |
&= | Concatenates a String expression to a String variable or property and assigns the result to the variable or property. 将String表达式连接到String变量或属性,并将结果分配给变量或属性。 | STR1&= STR2赛车是一样的 STR1 = STR1与STR2 |
有很少其他重要的操作系统支持VB.Net。
运算符 | 描述 | 例 |
---|---|---|
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. 它应用于异步方法或lambda表达式中的操作数,以暂停该方法的执行,直到等待的任务完成。 | 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. 它返回指定类型的Type对象。 Type对象提供有关类型的信息,例如其属性,方法和事件。 | MsgBox(GetType(Integer).ToString()) |
Function Expression | It declares the parameters and code that define a function lambda expression. 它声明定义函数lambda表达式的参数和代码。 | 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. 它使用短路评估有条件地返回两个值之一。 可以使用三个参数或两个参数调用If运算符。 | Dim num = 5 Console.WriteLine(If(num >= 0, "Positive", "Negative")) |
运算符优先级确定表达式中的术语分组。 这会影响表达式的计算方式。 某些运算符比其他运算符具有更高的优先级; 例如,乘法运算符的优先级高于加法运算符:
例如,x = 7 + 3 * 2; 这里,x被分配13,而不是20,因为operator *具有比+高的优先级,所以它首先乘以3 * 2,然后加到7。
这里,具有最高优先级的运算符出现在表的顶部,具有最低优先级的运算符出现在底部。 在表达式中,将首先计算较高优先级运算符。
运算符 | 优先级 |
---|---|
Await | 最高 |
Exponentiation (^) | |
Unary identity and negation (+, -) | |
Multiplication and floating-point division (*, /) | |
Integer division (\) | |
Modulus arithmetic (Mod) | |
Addition and subtraction (+, -) | |
Arithmetic bit shift (<<, >>) | |
All comparison operators (=, <>, <, <=, >, >=, Is, IsNot, Like, TypeOf...Is) | |
Negation (Not) | |
Conjunction (And, AndAlso) | |
Inclusive disjunction (Or, OrElse) | |
Exclusive disjunction (Xor) | 最低 |