a) 点运算符 ( a.x ) 和索引运算符 ( a[x] ) 具有相同的优先级。因此,根据优先规则,表达式 a[x].SomeMember
计算为 (a[x]).SomeMember
而不是 (a.SomeMember ) [x]
?
b) 强制转换运算符是否为一元运算符(因此优先级低于点运算符),因此表达式 (int)a.x
的计算结果为 (int)(a.x)
?
谢谢
最佳答案
对于一个)
摘自 C# 规范第 7.2.1 节:
When an operand occurs between two operators with the same precedence, the associativity of the operators controls the order in which the operations are performed:
- Except for the assignment operators, all binary operators are left-associative, meaning that operations are performed from left to right. For example, x + y + z is evaluated as (x + y) + z.
- The assignment operators and the conditional operator (?:) are right-associative, meaning that operations are performed from right to left. For example, x = y = z is evaluated as x = (y = z).
这意味着在这种情况下,运算符将从左到右获得优先权。
b) 是的,这是正确的。这是一个强制转换表达式,如第 7.6.6 节所述,应用于一元表达式,并且强制转换按一元运算符(第 7.6 节)进行分类,并以相同的优先级进行处理。
关于c# - 根据什么优先规则,a[x].SomeMember 被评估为 (a[x]).SomeMember 而不是……?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2239500/