Csharp 简明教程

C

运算符是一个符号,它告诉编译器执行特定的数学或逻辑运算。C# 具有丰富的内置运算符且提供了以下类型的运算符 −

  1. Arithmetic Operators

  2. Relational Operators

  3. Logical Operators

  4. Bitwise Operators

  5. Assignment Operators

  6. Misc Operators

本教程逐一说明了算术、关系、逻辑、位运算、赋值和其他运算符。

Arithmetic Operators

下表显示了 C# 所支持的所有算术运算符。假设变量 A 存储 10 而变量 B 存储 20,则 −

Operator

Description

Example

+

Adds two operands

A + B = 30

-

从第一个操作数中减去第二个操作数

A - B = -10

*

Multiplies both operands

A * B = 200

/

Divides numerator by de-numerator

B / A = 2

%

模运算符,整数除法后的余数

B % A = 0

++

增量运算符将整数值增加 1

A++ = 11

 — 

减量运算符将整数值减少 1

A-- = 9

Relational Operators

下表显示了 C# 所支持的所有关系运算符。假设变量 A 存储 10 而变量 B 存储 20,则 −

Operator

Description

Example

==

检查两个操作数的值是否相等,如果相等,则条件变为真。

(A == B) 不为真。

!=

检查两个操作数的值是否相等,如果值不相等,则条件变为真。

(A != B) 为真。

>

检查左操作数的值是否大于右操作数的值,如果大于,则条件变为真。

(A > B) 不为真。

<

检查左操作数的值是否小于右操作数的值,如果小于,则条件变为真。

(A < B) 为真。

>=

检查左操作数的值是否大于或等于右操作数的值,如果大于或等于,则条件变为真。

(A >= B) 不为真。

检查左操作数的值是否小于或等于右操作数的值,如果小于或等于,则条件变为真。

(A ⇐ B) 为真。

Logical Operators

下表显示了 C# 所支持的所有逻辑运算符。假设变量 A 存储布尔值 true 而变量 B 存储布尔值 false,则 −

Operator

Description

Example

&&

称为逻辑 AND 运算符。如果两个操作数都是非零,则条件变为真。

(A && B) 为假。

称为逻辑 OR 运算符。如果两个操作数中的任何一个是非零,则条件变为真。

(A

B) is true.

!

称为逻辑 NOT 运算符。用于反转操作数的逻辑状态。如果条件为 true,则逻辑 NOT 运算符会将其变为 false。

Bitwise Operators

位运算符对位操作,并执行逐位操作。&、| 和 ^ 的真值表如下 −

p

q

p &

p

p ^ q

0

0

0

0

0

0

1

0

1

1

1

1

1

1

0

1

0

0

假设 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

The Bitwise operators supported by C# are listed in the following table. Assume variable A holds 60 and variable B holds 13, then −

link:../csharp/csharp_bitwise_operators.html[Show Examples]
[%autowidth]
|===

|Operator|Description|Example
|&|Binary AND Operator copies a bit to the result if it exists in both operands.|(A & B) = 12, which is 0000 1100
|||Binary OR Operator copies a bit if it exists in either operand.|(A | B) = 61, which is 0011 1101
|^|Binary XOR Operator copies the bit if it is set in one operand but not both.|(A ^ B) = 49, which is 0011 0001
|~|Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.|(~A ) = -61, which is 1100 0011 in 2's complement 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 = 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 = 15, which is 0000 1111
|===


=== Assignment Operators

There are following assignment operators supported by C# −

link:../csharp/csharp_assignment_operators.html[Show Examples]
[%autowidth]
|===

|Operator|Description|Example
|=|Simple assignment operator, Assigns values from right side operands to left side operand|C = A + B assigns value of A + B into C
|+=|Add AND assignment operator, It adds right operand to the left operand and assign 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 assign 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 assign 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 assign the result to left operand|C /= A is equivalent to C = C / A
|%=|Modulus AND assignment operator, It takes modulus using two operands and assign 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
|&=|Bitwise AND assignment operator|C &= 2 is same as C = C & 2
|^=|bitwise exclusive OR and assignment operator|C ^= 2 is same as C = C ^ 2
||=|bitwise inclusive OR and assignment operator|C |= 2 is same as C = C | 2
|===


=== Miscellaneous Operators

There are few other important operators including *sizeof, typeof* and *? :* supported by C#.

link:../csharp/csharp_misc_operators.html[Show Examples]
[%autowidth]
|===

|Operator|Description|Example
|sizeof()|Returns the size of a data type.|sizeof(int), returns 4.
|typeof()|Returns the type of a class.|typeof(StreamReader);
|&|Returns the address of an variable.|&a; returns actual address of the variable.
|*|Pointer to a variable.|*a; creates pointer named 'a' to a variable.
|? :|Conditional Expression|If Condition is true ? Then value X : Otherwise value Y
|is|Determines whether an object is of a certain type.|If( Ford is Car) // checks if Ford is an object of the Car class.
|as|Cast without raising an exception if the cast fails.|Object obj = new StringReader("Hello"); StringReader r = obj as StringReader;

|===


=== Operator Precedence in C

Operator precedence determines the grouping of terms in an expression. This affects evaluation of an expression. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator.

For example x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so the first evaluation takes place for 3*2 and then 7 is added into it.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators are evaluated first.

link:../csharp/csharp_operators_precedence.html[Show Examples]
[%autowidth]
|===

|Category|Operator|Associativity
|Postfix|() [] -> . ++ - -|Left to right
|Unary|+ - ! ~ ++ - - (type)* & sizeof|Right to left
|Multiplicative|* / %|Left to right
|Additive|+ -|Left to right
|Shift|<< >>|Left to right
|Relational|< <= > >=|Left to right
|Equality|== !=|Left to right
|Bitwise AND|&|Left to right
|Bitwise XOR|^|Left to right
|Bitwise OR|||Left to right
|Logical AND|&&|Left to right
|Logical OR||||Left to right
|Conditional|?:|Right to left
|Assignment|= += -= *= /= %=>>= <<= &= ^= |=|Right to left
|Comma|,|Left to right
|===