gpt4 book ai didi

c# - 基于什么优先规则 a[x].SomeMember 被评估为 (a[x]).SomeMember 而不是……?

转载 作者:行者123 更新时间:2023-11-30 21:22:32 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com