📜  VB.Net-运营商

📅  最后修改于: 2020-11-19 08:48:54             🧑  作者: Mango


运算符是一个符号,告诉编译器执行特定的数学或逻辑操作。 VB.Net内置丰富的运算符,并提供以下类型的常用运算符-

  • 算术运算符

  • 比较运算符

  • 逻辑/按位运算符

  • 移位运算符

  • 赋值运算符

  • 杂项运算符

本教程将解释最常用的运算符。

算术运算符

下表显示了VB.Net支持的所有算术运算运算符。假设变量A保持2,变量B保持7,则-

显示范例

Operator Description Example
^ Raises one operand to the power of another B^A will give 49
+ Adds two operands 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,则-

显示范例

Operator Description Example
= Checks if the values of two operands are equal or not; if yes, then condition becomes true. (A = B) is not true.
<> Checks if the values of two operands are equal or not; if values are not equal, then condition becomes true. (A <> B) is true.
> Checks if the value of left operand is greater than the value of right operand; if yes, then condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand; if yes, then condition becomes true. (A < B) is true.
>= 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) is not true.
<= 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) is true.

除了以上的,VB.Net提供了三个比较运算符,我们将使用在即将到来的章节;但是,我们在这里进行简要说明。

  • 运算符-它比较两个对象引用变量,并确定两个对象引用是否引用同一对象而不执行值比较。如果object1和object2都引用完全相同的对象实例,则结果为True ;否则,结果为False。

  • IsNot Operator-还比较两个对象引用变量,并确定两个对象引用是否引用了不同的对象。如果object1和object2都引用完全相同的对象实例,则结果为False ;否则,结果为True。

  • 运算符-将字符串与模式进行比较。

逻辑/按位运算符

下表显示了VB.Net支持的所有逻辑运算符。假设变量A保持布尔值True,变量B保持布尔值False,则-

显示范例

Operator Description Example
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. (A And B) is False.
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 Or B) is True.
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. Not(A And B) is True.
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. A Xor B is True.
AndAlso It is the logical AND operator. It works only on Boolean data. It performs short-circuiting. (A AndAlso B) is False.
OrElse It is the logical OR operator. It works only on Boolean data. It performs short-circuiting. (A OrElse B) is True.
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

我们已经看到VB.Net支持的按位运算符是And,Or,Xor和Not。左移和右移的位移位运算符分别为>>和<<。

假设变量A保持60,变量B保持13,则-

显示范例

Operator Description Example
And Bitwise AND Operator copies a bit to the result if it exists in both operands. (A AND B) will give 12, which is 0000 1100
Or Binary OR Operator copies a bit if it exists in either operand. (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. (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

赋值运算符

VB.Net支持以下赋值运算符-

显示范例

Operator Description Example
= Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assign value of A + B into C
+= Add AND assignment operator, It adds right operand to the left operand and assigns the result to left operand C += A is equivalent to C = C + A
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assigns the result to left operand C -= A is equivalent to C = C – A
*= Multiply AND assignment operator, It multiplies right operand with the left operand and assigns the result to left operand C *= A is equivalent to 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) C /= A is equivalent to C = C / A
\= Divide AND assignment operator, It divides left operand with the right operand and assigns the result to left operand (Integer division) C \= A is equivalent to 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 is equivalent to C = C ^ A
<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
&= Concatenates a String expression to a String variable or property and assigns the result to the variable or property.

Str1 &= Str2 is same as

Str1 = Str1 & Str2

杂项运算符

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"))

VB.Net中的运算符优先级

运算符优先级确定表达式中术语的分组。这会影响表达式的求值方式。某些运算符具有更高的优先级;例如,乘法运算符的优先级比加法运算符-

例如,x = 7 + 3 * 2;在这里,x被赋值为13,而不是20,因为运算符*的优先级比+高,因此它首先与3 * 2相乘,然后加到7。

在此,优先级最高的运算符出现在表格的顶部,而优先级最低的运算符出现在表格的底部。在表达式中,优先级较高的运算符将首先被评估。

显示范例

Operator Precedence
Await Highest
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) Lowest