为什么后缀++ / - 归类为C#的主要运营商? [英] Why are Postfix ++/-- categorized as primary Operators in C#?

查看:186
本文介绍了为什么后缀++ / - 归类为C#的主要运营商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我教一类的C ++程序员的C#语言的基础知识。正如我们讨论的话题运营商我使用的主,一元等运营商的C#标准类别。

Currently I'm teaching a class of C++ programmers the basics of the C# language. As we discussed the topic operators I used C# standard categories of primary, unary etc. operators.

一位与会者感到不解,因为在C#标准后缀+ + / - 已投入在主运营商的类别,而不是前缀+ + / - 。她的这种混乱背后的理由是,她宁愿实现C ++运算符后缀++ / - 的的操作前缀++ / - 的条款。换句话说,她宁愿指望运营商前缀++ / - 作为一个主要的运营商。 - 我明白了她的观点,但我不能给她背后的理由。确定的运算符后缀++ / - 具有比前缀+ + / - 的优先级高?,但这是后面的唯一理

One of the attendees felt puzzled, because in the C# standard the "postfix ++/--" have been put in the category of primary operators rather than the "prefix ++/--". Her rationale behind this confusion was, that she would rather implement the C++ operator "postfix ++/--" in terms of the operator "prefix ++/--". In other words she would rather count the operator "prefix ++/--" as a primary operator. - I understand her point, but I can't give to her a rationale behind that. OK the operators "postfix ++/--" have a higher precedence than "prefix ++/--", but is this the only rationale behind that?

的。规范章节14.2.1运算符优先级和结合提到

The spec mentioned it in section "14.2.1 Operator precedence and associativity".

所以,我很中性的问题:为什么后缀++ / - 归类为C#的主要运营商?
是有它更深的道理吗?

So my very neutral question: Why are Postfix ++/-- categorized as primary Operators in C#? Is there a deeper truth in it?

推荐答案

由于ECMA标准本身并没有定义什么是主要运算符,比位次(即来临前'一元')不可能有其他的意义等。词的选择可能是坏的。

Since the ECMA standard itself does not define what a 'Primary' operator is, other than order of precedence (i.e. coming before 'Unary') there can be no other significance. The choice of words was probably bad.

考虑到,在许多C-链接语言,后缀运营商往往会创造一个表达式的中间结果存储在临时变量(请参阅:首选运营商前缀后缀上以分号)。因此,他们是由前缀版本有着根本的不同。

Take into account that in many C-link languages, postfix operators tend to create a temporary variable where the expression's intermediate result is stored (see: "Prefer prefix operators over postfix" at Semicolon). Thus, they are fundamentally different from the prefix version.

不过,快速检查单和Visual Studio如何编译for循环使用后缀和前缀形式,我看到产生的IL代码是相同的。只有当你使用后缀/前缀表达式的值它翻译成不同的IL(仅影响其中的'DUP'指令放置),至少在这些实现提及。

Nonetheless, quickly checking how Mono and Visual Studio compile for-loops using the postfix and prefix forms, I saw that the IL code produced is identical. Only if you use the postfix/prefix expression's value does it translate to different IL (only affecting where the 'dup' instruction in placed), at least with those implementations mentioned.

这篇关于为什么后缀++ / - 归类为C#的主要运营商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆