Vb.net-operators
VB.Net-オペレーター
演算子は、特定の数学的または論理的な操作を実行するようコンパイラーに指示する記号です。 VB.Netは組み込みの演算子が豊富であり、次のタイプの一般的に使用される演算子を提供します-
- 算術演算子
- 比較演算子
- 論理演算子/ビット演算子
- ビットシフト演算子
- 割り当て演算子
- その他の演算子
このチュートリアルでは、最も一般的に使用される演算子について説明します。
算術演算子
次の表は、VB.Netでサポートされているすべての算術演算子を示しています。 変数 A が2を保持し、変数 B が7を保持すると仮定します-
リンク:/vb.net/vb.net_arithmetic_operators [例を表示]
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を保持すると仮定します-
リンク:/vb.net/vb.net_comparison_operators [例を表示]
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はさらに3つの比較演算子を提供します。これは、今後の章で使用します。ただし、ここでは簡単に説明します。
- Is 演算子-2つのオブジェクト参照変数を比較し、値の比較を実行せずに2つのオブジェクト参照が同じオブジェクトを参照しているかどうかを判断します。 object1とobject2の両方がまったく同じオブジェクトインスタンスを参照する場合、結果は True です。それ以外の場合、結果はFalseです。
- IsNot 演算子-また、2つのオブジェクト参照変数を比較し、2つのオブジェクト参照が異なるオブジェクトを参照しているかどうかを判断します。 object1とobject2の両方がまったく同じオブジェクトインスタンスを参照している場合、結果は False です。それ以外の場合、結果はTrueです。
- Like 演算子-文字列をパターンと比較します。
論理演算子/ビット演算子
次の表は、VB.Netでサポートされているすべての論理演算子を示しています。 変数Aがブール値Trueを保持し、変数Bがブール値Falseを保持すると仮定します-
リンク:/vb.net/vb.net_logical_operators [例を表示]
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 |
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でサポートされているBitwise演算子は、And、Or、Xor、Notであることがわかりました。 ビットシフト演算子は、左シフトと右シフトのそれぞれ>>と<<です。
変数Aが60を保持し、変数Bが13を保持すると仮定します-
リンク:/vb.net/vb.net_bitshift_operators [例を表示]
[cols=",,",options="header",]
|===
|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でサポートされている次の割り当て演算子があります-
リンク:/vb.net/vb.net_assignment_operators [例を表示]
[width="100%",cols="34%,33%,33%",options="header",]
|===
|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. a| Str1 &= Str2 is same as
Str1 = Str1&Str2
|===
=== その他の演算子
VB.Netでサポートされている他の重要な演算子はほとんどありません。
リンク:/vb.net/vb.net_misc_operators [例を表示]
[width="100%",cols="34%,33%,33%",options="header",]
|===
|Operator |Description |Example |AddressOf |Returns the address of a procedure. a|
[source,prettyprint,notranslate]
----
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. a|
[source,prettyprint,notranslate]
----
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. a|
[source,prettyprint,notranslate]
----
MsgBox(GetType(Integer).ToString())
----
|Function Expression |It declares the parameters and code that define a function lambda expression. a|
[source,prettyprint,notranslate]
----
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. a|
[source,prettyprint,notranslate]
----
Dim num = 5
Console.WriteLine(If(num >= 0,
"Positive", "Negative"))
----
|===
=== VB.Netでの演算子の優先順位
演算子の優先順位は、式内の用語のグループ化を決定します。 これは、式の評価方法に影響します。 特定の演算子は、他の演算子よりも優先順位が高くなっています。たとえば、乗算演算子は加算演算子よりも優先順位が高い-
たとえば、x = 7 + 3* 2;ここでは、演算子*の優先順位が+よりも高いため、xには20ではなく13が割り当てられます。したがって、最初に3 * 2で乗算され、7に加算されます。
ここでは、優先順位が最も高い演算子が表の上部に表示され、優先順位が最も低い演算子が下部に表示されます。 式内では、優先順位の高い演算子が最初に評価されます。
リンク:/vb.net/vb.net_operators_precedence [例を表示]
[cols=",",options="header",]
|===
|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
|===