[英]Why does '+' + a short convert to 44
I have a line of code that looks like this: 我有一行代码看起来像这样:
MyObject.PhoneNumber = '+' + ThePhonePrefix + TheBizNumber;
Basically, I'm creating a phone number in E164 format and then I assign that string to a string property of an object. 基本上,我正在创建E164格式的电话号码,然后将该字符串分配给对象的字符串属性。 ThePhonePrefix is a short that holds the international phone prefix and TheBizNumber is a string that holds the phone number digits.
ThePhonePrefix是保存国际电话前缀的缩写,TheBizNumber是保存电话号码数字的字符串。
Why didn't the compiler bug when I was concatenating a short in the string in the first place? 当我首先在字符串中串联一个短字符串时,为什么没有编译器错误? And then why does '+' + 1 equal 44??
那为什么'+'+ 1等于44呢? This was a pretty hard bug to track because there was no compile error and 44 is the phone prefix for the UK so everything "looked" like it was working because the client-side code just saw a UK number.
这是一个很难跟踪的错误,因为没有编译错误,并且44是UK的电话前缀,所以所有“看上去”都像在起作用,因为客户端代码只看到了UK数字。 Why 44?
为什么是44?
43 is the ( http://www.asciitable.com/ ) ascii value of the character '+'. 43是字符“ +”的( http://www.asciitable.com/)ascii值。 The compiler interprets
char
addition as, well, addition, rather than concatenation. 编译器将
char
加法解释为加法,而不是串联。 Try "+"
instead to get the expected string behavior. 尝试使用
"+"
代替以获得预期的字符串行为。
Why didn't the compiler bug when I was concatenating a short in the string in the first place?
当我首先在字符串中串联一个短字符串时,为什么没有编译器错误?
String concatenation using +
sign internally calls string.Concat
, which internally calls ToString
on each parameter. 使用
+
符号的字符串连接内部调用string.Concat
,内部对每个参数调用ToString
。 Hence no error. 因此没有错误。
why does '+' + 1
为什么'+'+ 1
You are doing character/numeric arithmetic. 您正在执行字符/数字算术运算。
43
being value of +
and short/int 1
is 44. +
43
,而short / int 1
为44。
Because of operator + associativity from left to right it is first character/numeric addition and then string concatenation. 由于运算符+从左到右的关联性,它首先是字符/数字加法,然后是字符串串联。
So it is like: 就像这样:
MyObject.PhoneNumber = ('+' + ThePhonePrefix) + TheBizNumber;
You can use "+"
to mark it as a string or explicitly call String.Concat
like: 您可以使用
"+"
将其标记为字符串,也可以显式调用String.Concat
例如:
var result = string.Concat('+', ThePhonePrefix, TheBizNumber);
'+'
is the +
character. '+'
是+
字符。
"+"
is the +
string. "+"
是+
字符串。
When using the +
operator to add/concatenate, the +
character ( '+'
) will convert to an integer. 当使用
+
运算符添加/连接时, +
字符( '+'
)将转换为整数。
When using the +
operator to add/concatenate, the +
string ( "+"
) will convert the other operands to a string. 当使用
+
运算符添加/连接时, +
字符串( "+"
)会将其他操作数转换为字符串。
What you wrote equates to: 您写的内容等同于:
MyObject.PhoneNumber = (int)'+' + ThePhonePrefix + TheBizNumber;
What you meant to write was: 您的意思是:
MyObject.PhoneNumber = "+" + ThePhonePrefix + TheBizNumber;
...although it may be more clear if you wrote it using a format string (also it's easier to extend later without making arithmetic errors: ...虽然使用格式字符串编写它可能会更清楚(也可以稍后扩展而不会产生算术错误,这更容易:
MyObject.PhoneNumber = string.Format("+{0}{1}", ThePhonePrefix, TheBizNumber);
I like to note in these situations that the core issue is that the +
operator is overloaded. 我想在这些情况下指出,核心问题是
+
运算符已重载。 It performs both addition as well as string concatenation. 它执行加法和字符串连接。 Some languages avoid this issue by using a separate operator.
某些语言通过使用单独的运算符来避免此问题。 PHP is a great example for this case (a rare occurrence for sure) in that it uses
+
for addition, and .
PHP是这种情况的一个很好的例子(肯定是罕见的情况),因为它使用
+
来加法,和.
for concatenation. 用于串联。
There are a lot of overloads of the +
operator (see the specs for a list of all of them). +
运算符有很多重载(有关所有列表的详细信息,请参见规范)。 There are two of them that are relevant to the example at hand: 其中有两个与手头示例相关:
operator +(string, object)
operator +(int, int)
Since ThePhonePrefix
is an int, it is implicitly convertible to either of these overloads. 由于
ThePhonePrefix
是int,因此可以隐式转换为这些重载之一。 It, like all objects, is implicitly convertible to object
(through a boxing operation in this case) and it is exactly an int
, so it matches that overload without needing any conversion. 与所有对象一样,它可以隐式转换为
object
(在这种情况下通过装箱操作),并且它恰好是int
,因此它与该重载匹配,而无需任何转换。
'+'
is a char, which has an implicit conversion to int
(it will use the numeric value of the character as is defined in the UTF-16 specs, in this case, it's 43). '+'
是一个字符,它隐式转换为int
(它将使用UTF-16规范中定义的字符的数值,在这种情况下为43)。 It's not implicitly convertible to string
. 它不能隐式转换为
string
。 So the (int, int)
overload matches and performs integer addition. 因此
(int, int)
重载匹配并执行整数加法。
Were you to use "+"
instead of '+'
(a string instead of a char) then that value would not be implicitly convertible to int
(string has no implicit conversion to int
) and it would be convertible to string
(since it is a string). 如果您使用
"+"
而不是'+'
(字符串而不是char),则该值将不能隐式转换为int
(字符串没有隐式转换为int
),并且可以将其转换为string
(因为它是一个字符串)。 That would match the first overload I mentioned, and perform string concatenation on the two objects. 这将与我提到的第一个重载相匹配,并对两个对象执行字符串连接。
It's a character, not a string. 这是一个字符,而不是字符串。 String operator + does concatenation.
字符串运算符+进行串联。 char operator + does addition.
字符运算符+做加法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.