C # Why does & #39; + & #39; + a short convert to 44,

Source: Internet
Author: User

C # 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?

Thanks.


Answer:

Why didn't the compiler bug when I was concatenating a short in the string in the first place?

String concatenation using+Sign internally cballsstring.Concat, Which internally CILSToStringOn each parameter. Hence no error.

Why does '+ 1

You are doing character/numeric arithmetic.43Being value+And short/int1Is 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 callString.ConcatLike:

var result = string.Concat('+', ThePhonePrefix, TheBizNumber);
Web: http://stackoverflow.com/questions/29397495/why-does-a-short-convert-to-44

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.