📅  最后修改于: 2020-10-30 07:34:31             🧑  作者: Mango
运算符只是用于执行操作的符号。可以有多种类型的运算,例如算术运算,逻辑运算,按位运算等。
有以下几种类型的运算符可以用C#语言执行不同类型的操作。
运算符的优先级指定将首先评估哪个运算符。关联性指定要评估的运算符方向,可以从左到右或从右到左。
让我们通过以下示例了解优先级:
int data= 10+ 5*5
“ data”变量将包含35,因为*(乘法运算符)在+(加法运算符)之前求值。
C#运算符的优先级和关联性如下所示:
Category (By Precedence) | Operator(s) | Associativity |
---|---|---|
Unary | + – ! ~ ++ — (type)* & sizeof | Right to Left |
Additive | + – | Left to Right |
Multiplicative | % / * | Left to Right |
Relational | < > <= >= | Left to Right |
Shift | << >> | Left to Right |
Equality | == != | Right to Left |
Logical AND | & | Left to Right |
Logical OR | | | Left to Right |
Logical XOR | ^ | Left to Right |
Conditional OR | || | Left to Right |
Conditional AND | && | Left to Right |
Null Coalescing | ?? | Left to Right |
Ternary | ?: | Right to Left |
Assignment | = *= /= %= += – = <<= >>= &= ^= |= => | Right to Left |