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

标签 c# .net clr

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/

相关文章:

c# - MVVM - 处理需要在 ViewModel 表示的基础模型上执行的操作

.net - 如何在 F# 中学习 GUI 编程

asp.net - 在x64和x86环境中使用PresentationCore和WindowsBase dll

c++ - 链接 C++ 和 C++/CLI 项目 (.NET Core) 时导入错误 (E0337)

c# - 全屏 c# windows 窗体覆盖所有显示器

c# - 将复杂对象的 iQueryable 导出到 Excel

c# - 如何从 Excel VBA 调用 .NET 方法?

c# - 构造函数是初始化值还是 CLR?

c# - C# 中的三元运算符

c# - 转向MVVM之前需要WPF知识