简体   繁体   中英

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. ThePhonePrefix is a short that holds the international phone prefix and TheBizNumber is a string that holds the phone number digits.

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?? 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. Why 44?

在此处输入图片说明

43 is the ( http://www.asciitable.com/ ) ascii value of the character '+'. The compiler interprets char addition as, well, addition, rather than concatenation. 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. Hence no error.

why does '+' + 1

You are doing character/numeric arithmetic. 43 being value of + and short/int 1 is 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:

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 . 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. 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.

'+' 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). It's not implicitly convertible to string . So the (int, int) overload matches and performs integer addition.

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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM