VB.Net - 运算符

2021-08-24 09:30 更新

• 算术运算符

• 比较运算符

• 逻辑/位运算符

• 位移位运算符

• 赋值运算符

• 其他运算符

算术运算符

^

Raises one operand to the power of anothe

B^A will give 49
+

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
MODModulus Operator and remainder of after an integer division

B MOD A will give 1

比较运算符

=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）为真。

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

2、IsNot运算符 - 它还比较两个对象引用变量，并确定两个对象引用是否引用不同的对象。 如果object1和object2都引用完全相同的对象实例，则result为False; 否则，result为True。

3、Like运算符 - 它将字符串与模式进行比较。

逻辑/位运算符

AndIt 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和B）为假。
OrIt 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）为真。
NotIt 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.

XorIt 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.

AndAlsoIt is the logical AND operator. It works only on Boolean data. It performs short-circuiting.

（A AndAlso运算B）为假。
OrElseIt is the logical OR operator. It works only on Boolean data. It performs short-circuiting.

（A OrElse运算B）为真。
IsFalseIt determines whether an expression is False.

IsTrueIt determines whether an expression is True.

位移运算符

pqp＆Qp | qp ^ Q
00000
01011
11110
10011

A = 0011 1100

B = 0000 1101

-----------------

A＆B = 0000 1100

A | B = 0011 1101

A ^ B = 0011 0001

〜A = 1100 0011

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

=

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

C + = A等于C = C + A
- =Subtract AND assignment operator, It subtracts right operand from the left operand and assigns the result to left operand

Ç - = A等于C = C - A
* =Multiply AND assignment operator, It multiplies right operand with the left operand and assigns the result to left operand

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)

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)

ç = 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

C语言的<< = 2是同C = C << 2
>> =Right shift AND assignment operator

C >> = 2 >> 2同C = C
＆=Concatenates a String expression to a String variable or property and assigns the result to the variable or property.

STR1＆= STR2赛车是一样的

STR1 = STR1与STR2

其他运算符

```AddHandler Button1.Click,
AwaitIt 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()
```
GetTypeIt 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 ExpressionIt declares the parameters and code that define a function lambda expression.

```Dim add5 = Function(num As
Integer) num + 5
'prints 10
```
IfIt 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中的运算符优先级

Await最高
Exponentiation (^)
Unary identity and negation (+, -)
Multiplication and floating-point division (*, /)
Integer division (\)
Modulus arithmetic (Mod)
Arithmetic bit shift (<<, >>)
All comparison operators (=, <>, <, <=, >, >=, Is, IsNot, Like, TypeOf...Is)
Negation (Not)
Conjunction (And, AndAlso)
Inclusive disjunction (Or, OrElse)
Exclusive disjunction (Xor)最低

App下载