c# - Why does '+' + a short convert to 44 -


i have line of code looks this:

myobject.phonenumber = '+' + thephoneprefix + thebiznumber; 

basically, i'm creating phone number in e164 format , assign string string property of object. thephoneprefix short holds international phone prefix , thebiznumber string holds phone number digits.

why didn't compiler bug when concatenating short in string in first place? , why '+' + 1 equal 44?? pretty hard bug track because there no compile error , 44 phone prefix uk "looked" working because client-side code saw uk number. why 44?

thanks.

enter image description here

why didn't compiler bug when concatenating short in string in first place?

string concatenation using + sign internally calls string.concat, internally calls tostring on each parameter. hence no error.

why '+' + 1

you doing character/numeric arithmetic. 43 being value of + , short/int 1 44.

because of operator + associativity left right first character/numeric addition , string concatenation.

so like:

myobject.phonenumber = ('+' + thephoneprefix) + thebiznumber; 

you can use "+" mark string or explicitly call string.concat like:

var result = string.concat('+', thephoneprefix, thebiznumber); 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -