C#的+=运算符两例

刚偶尔看到了justjavac写的java解惑 - 半斤八两(一)和java解惑 - 半斤八两(二)。里面提到了Java的复合赋值运算符的两个陷阱:1) 复 合赋值运算符有强制类型转换的语义;2) += 左侧必须是原始类型中的数字类型,或者是String类型。

JLS3e如是说:

Java Language Specification, 3rd Edition 写道

15.26.2 Compound Assignment Operators

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

核心就这么一句,后面还有一堆细节规定,有兴趣可以到官网阅读:JLS3e: 15.26.2 Compound Assignment Operators

读下来,只有当复合赋值运算符的左手边是数组,且数组元素类型是String时才有提到特别针对 String的+=,在15.26.2的其它地方并没有提到justjavac说的第二个限制,怪哉。

ECMA-334如是说:

ECMA-334, 4th Edition 写道

14.14.2 Compound assignment

An operation of the form x op= y is processed by applying binary operator overload resolution (§14.2.4) as if the operation was written x op y. Then,  

If the return type of the selected operator is implicitly convertible to the type of x, the operation is evaluated as x = x op y, except that x is evaluated only once.

Otherwise, if the selected operator is a predefined operator, if the return type of the selected operator is explicitly convertible to the type of x, and if y is implicitly convertible to the type of x or the operator is a shift operator, then the operation is evaluated as x = (T)(x op y), where T is the type of x, except that x is evaluated only once.

Otherwise, the compound assignment is invalid, and a compile-time error occurs.

The term “evaluated only once” means that in the evaluation of x op y, the results of any constituent expressions of x are temporarily saved and then reused when performing the assignment to x. [Example: In the assignment A()[B()] += C(), where A is a method returning int[], and B and C are methods returning int, the methods are invoked only once, in the order A, B, C. end example]

When the left operand of a compound assignment is a property access or indexer access, the property or indexer shall have both a get accessor and a set accessor. If this is not the case, a compile-time error occurs.

The second rule above permits x op= y to be evaluated as x = (T)(x op y) in certain contexts. The rule exists such that the predefined operators can be used as compound operators when the left operand is of type sbyte, byte, short, ushort, or char. Even when both arguments are of one of those types, the predefined operators produce a result of type int, as described in §14.2.6.2. Thus, without a cast it would not be possible to assign the result to the left operand.

The intuitive effect of the rule for predefined operators is simply that x op= y is permitted if both of x op y and x = y are permitted. [Example: In the following code

C#代码

byte b = 0;
char ch = '\0';
int i = 0;
b += 1;    // Ok
b += 1000;      // Error, b = 1000 not permitted
b += i;        // Error, b = i not permitted
b += (byte)i;    // Ok
ch += 1;       // Error, ch = 1 not permitted
ch += (char)1;   // Ok

the intuitive reason for each error is that a corresponding simple assignment would also have been an error. end example]

[Note: Compound assignment operations support lifted operators. Since a compound assignment x op= y is evaluated as either x = x op y or x = (T)(x op y), the rules of evaluation implicitly cover lifted operators. end note]

14.14.3 Event assignment

If the left operand of a += or -= operator is an event, the expression is classified as an event access, and is evaluated as follows:

The instance expression, if any, of the event access is evaluated.

The right operand of the += or -= operator is evaluated, and, if required, converted to the type of the left operand through an implicit conversion (§13.1).

An event accessor of the event is invoked, with argument list consisting of the value computed in the previous step. If the operator was +=, the add accessor is invoked; if the operator was -=, the remove accessor is invoked.

An event assignment expression does not yield a value. Thus, an event assignment expression is valid only in the context of a statement-expression (§15.6).

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索lift and throw
, 运算符
, type
, operator
, operator overloading
, assign
, is
, of
, The
, Operators
C#正则用法两例
c站、c语言、cf、ch、c罗,以便于您获取更多的相关知识。

时间: 2024-10-02 15:19:00

C#的+=运算符两例的相关文章

无刷新显示即时更新数据两例

数据|刷新|无刷新|显示 因最近做网站需要用到无刷新数据,所以在网上查了一些资料,无刷新数据的实现无外乎用javascript或xmlhttp或iframe来实现.在网上找到一代码不是不能用就是效率太低,有的甚至使我的CPU达到100%,我根据网上的代码及查阅了一些资料,提供以下两例无刷新数据的例子,保证完全无错! 例一:example1.htm-------------------------------------<html><head><title>无刷新<

Windows 2000怪异故障两例

我们单位是一所医疗机构,整个医院的网络是由一台安装有WIN2000 服务器版的惠普服务器及数十台以WIN98作平台的终端微机组成.数据库系统采用微软公司的SQL 7.0版本,开发工具为SYBASE 公司的PowerBuild 6.0.在近一年的运行中,出现过两例怪异的"故障",几乎导致系统瘫痪,在此将详细过程叙述如下,以期对大家有所启示和借鉴. "故障"一:系统运行几个月后,整个管理系统的运行速度变得非常慢,无论是挂号还是划价发药都反应迟钝,导致全院无法进行正常工作

Linux/Unix的精巧约定两例及其简析:目录权限和文本行数

学玩*nux时候,碰到的一些问题,弄明白了后也就过去了.今天看到旁边的同学对目录权限有些模糊,给解释了一下.想想不如把这些问题都记下来. 设计其实包含的是一套约定.能运行.解决问题的约定都是可用的约定.但解决的多种约定方式或说是设计中,作一些比较可以感觉到哪个会更统一更简单.下文提到的两例Linux/Unix约定说明后,我也简单分析一下约定,找出看似复杂或是不直觉约定中内部包含的简洁统一. 清楚的同学,就当是个活动脑子的问题,过一遍小乐一下  如有谬误敬请指正. 目录权限的约定 目录的执行权限

激光打印机出现无字迹故障维修两例_硬件维护

激光打印机故障维修两例 故障现象一: 一台惠普HP LaserJet 1010激光打印机使用一段时间后,联机打印时,进纸正常,但打印无字迹. 分析与维修: 我们知道,惠普HP LaserJet 1010激光打印机是目前一款采用惠普独有的瞬时热熔器技术,高达12 ppm(A4, letter)打印速度:应用惠普分辨率增强技术(REt), 分辨率高达600 dpi:月打印负荷高达5,000页,8 MB RAM的主打产品,在当今党政机关.大中型企事业单位.金融证券服务业等领域均占有一定的比例. 下面,

TopList标签和JavaScript结合两例_javascript技巧

 (SteamCMS)Fish模版专贴一:TopList标签和JavaScript结合两例 首先可以先参考 SteamCMS 标准模版标签说明 来了解一下TopList标签及其属性. 在Top列表的记录前加上数字(当然也可以是图片,这里只是简单的举个例子). 原理说明:先在最前面初始化一个js变量,在TopList中间,将Js变量加1,并在TopList循环体中间输出(我写asp代码一样的道理) 关键代码:  <body> <script language="javascrip

菜鸟必读之网络故障两例_网络冲浪

最近,笔者在学校网络的维护过程中碰到了两例并不常见的网络故障,但却非常有意思,觉得有必要把它们拿出来供大家参考. 第一例故障:客户机不能即时自动从DHCP服务器上获得它的TCP/IP配置 我们学校的IP地址.DNS和网关都是通过DHCP服务器自动获得的.前段时间,学校领导觉得通过教育城域网上网速度不够快,就决定直接通过电信专线上网,这样一来我就修改了DHCP服务器的DNS设置,并且将DHCP服务器重启.之后有少数几个教师向我反映他们的电脑打不开网页了,但是QQ都能上.排除了病毒.系统的原因后,我

澳大利亚新确诊两例输入性甲型H1N1流感病例

中新网5月20日电 据美联社报道,澳大利亚卫生当局20日新确诊两例输入性甲型H1N1流感病患,令该国病患总数增至3人. 维多利亚省卫生部长安德鲁说,新增的一名病患是从美国旅游归来的9岁男童. 目前该名患者正在家中进行康复治疗,病情较轻尚不需入院.他的家人也出现疑似症状,在家中进行隔离观察,正在服用抗病毒药. 此外,悉尼一名自美国归来的妇女也确诊患上甲型H1N1流感,但病情较轻.

忽然一周:流言二重奏 新营销两例

Howtimeflies! 一周就这么过去了,我在广渠门桥的咖啡馆记录下这一周给我留下印象的五件事情. 流言二重奏: 1.腾讯洽购腾讯携程.你有没有发现,现在群众 普遍喜欢传播流言,对于企业的正式声明反而没有人去传播和关注? 媒体报道腾讯以估值60亿美金准备洽购携程以获得控股权,随后无数分析师开始分析此交易的可能性,不少朋友也开始以转发求辟谣的方式表达了自己对此流言的赞叹. 数小时后,携程方面正式发布声明,称目前没有和腾讯产生资本方面的任何谈判,流言存活不到5小时被击破.腾讯也以匿名高管的方式对

学Silverlight 2系列(33):Silverlight 2应用Web Service两例

概述 我们知道,在Silverlight 2中提供了丰富的网络通信API,包括支持SOAP服 务.REST服务.基于HTTP通信.Socket通信等.本文我将通过几个示例来演示如 何在Silverlight 2中应用Web Service实现文件上传和电子邮件发送. 使用Web Service上传文件 我将通过一个示例来展示如何使用Web Service向服务器上传文件,首先创建 Silverlight项目,并在Web测试项目中添加一个ASP.NET Web Service文件.现 在来实现相关