C# - 运算符

本文关键字:运算符 | 更新日期: 2023-09-12 17:38:37

C# 中的运算符是一些对操作数执行某些操作的特殊符号。在数学中,加号 (+) 表示左右数字的总和。同样,C# 包含用于不同类型操作的各种运算符。

下面的示例演示 C# 中的 + 运算符。

Example: + Operator

int x = 5 + 5;
int y = 10 + x;
int z = x + y;

在上面的示例中,+ 运算符将两个数字文字相加,并将结果分配给一个变量。它还将两个 int 变量的值相加,并将结果分配给一个变量。

某些运算符的行为因操作数的类型而异。例如,+ 运算符连接两个字符串。

Example: + Operator with Strings

string greet1 = "Hello " + "World!";
string greet2 = greeting + name;

注意:C# 中有两种类型的运算符,一元运算符和二进制运算符。一元运算符作用于单个操作数,而二元运算符作用于两个操作数(运算符的左侧和右侧操作数)。

C# 包括以下类别的运算符:

  • 算术运算符
  • 赋值运算符
  • 比较运算符
  • 相等运算符
  • 布尔逻辑运算符
  • 移位运算符
  • 成员访问运算符
  • 类型转换运算符
  • 指针相关运算符

算术运算符

算术运算符对所有数值类型操作数(如字节、字节、短、短、ushort、int、uint、长整型、长整型、ulong、浮点数、双精度数和十进制)执行算术运算。

Operator Name Description Example
+ Addition Computes the sum of left and right operands. int x = 5 + 5;
- Subtraction Subtract the right operand from the left operand int x = 5 - 1;
* Multiplication Multiply left and right operand int x = 5 * 1;
/ Division Divides the left operand by the right operand int x = 10 / 2;
% Remainder Computes the remainder after dividing its left operand by its right operand int x = 5 % 2;
++ Unary increment Unary increment ++ operator increases its operand by 1 x++
-- Unary decrement Unary decrement -- operator decreases its operand by 1 x--
+ Unary plus Returns the value of operand +5
- Unary minus Computes the numeric negation of its operand. -5

赋值运算符

赋值运算符 = 将其右侧具有值分配给其左侧变量、属性或索引器。它还可以与其他算术、布尔逻辑和按位运算符一起使用。

Operator Name Description Example
= Assignment Assigns its right had value to its left-hand variable, property or indexer. x = 10;
x op= y Compound assignment Short form of x =x op y where op = any arithmetic, Boolean logical, and bitwise operator. x += 5;
??= Null-coalescing assignment C# 8 onwards, ??= assigns value of the right operand only if the left operand is null x ??= 5;

比较运算符

比较运算符包含两个数字操作数并返回 true 或 false。

Operator Description Example
< Returns true if the left operand is less than the right operand x < y;
> Returns true if the left operand is greater than the right operand x > y;
<= Returns true if the left operand is less than or equal to the right operand x <= y
>= Returns true if the left operand is greater than or equal to the right operand x >= y;

相等运算符

相等运算符检查两个操作数是否相等。

Operator Description Example
== Returns true if operands are equal otherwise false. x == y;
!= Returns true if operands are not equal otherwise false. x != y;

布尔逻辑运算符

布尔逻辑运算符对布尔操作数执行逻辑运算。

Operator Description Example
! Reverses the bool result of bool expression. Returns false if result is true and returns true if result is false. !false
&& Computes the logical AND of its bool operands. Returns true both operands are true, otherwise returns false. x && y;
|| Computes the logical OR of its bool operands. Returns true when any one operand is true. x || y;
表达式中操作数的运算符计算

和优先级计算

从左到右开始。如果在表达式中使用多个运算符,则优先级较高的运算符先于优先级较低的运算符进行计算。

下表列出了从较高优先级运算符到较低优先级运算符的运算符。

Operators Category
x.y, x?.y, x?[y], f(x), a[i], x++, x--, new, typeof, checked, unchecked, default, nameof, delegate, sizeof, stackalloc, x->y Primary
+x, -x, !x, ~x, ++x, --x, ^x, (T)x, await, &x, *x, true and false Unary
x..y Range
x * y, x / y, x % y Multiplicative
x + y, x - y Additive
x << y, x >> y Shift
x < y, x > y, x <= y, x >= y, is, as Relational and type-testing
x == y, x != y Equality
x & y Boolean logical AND
x ^ y Boolean logical XOR
x | y Boolean logical OR
x && y Conditional AND
x || y Conditional OR
x ?? y Null-coalescing operator
c ? t : f Conditional operator
x = y, x += y, x -= y, x *= y, x /= y, x %= y, x &= y, x |= y, x ^= y, x <<= y, x >>= y, x ??= y, => Assignment and lambda declaration

下面的示例演示运算符优先级:

Example: Operator Precedence

int a = 5 + 3 * 3;
int b = 5 + 3 * 3 /  2; 
int c = (5 + 3) * 3 /  2; 
int d = (3 * 3) * (3 / 3 + 5);

详细了解C# 运算符