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 calls string.Concat,
which internally calls ToStringon
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);

website:http://stackoverflow.com/questions/29397495/why-does-a-short-convert-to-44

C# Why does '+' + a short convert to 44的更多相关文章

  1. WAV相关:从PCM16 Little Endian数据转WAV文件

    数据格式 [0.0, -0.0, -0.0, 0.0, 0.0, 0.0, 5.960464477539063e-08, 5.960464477539063e-08, 1.19209289550781 ...

  2. 进制转换及API接口中的转换

    //十进制转二进制Console.WriteLine("十进制166的二进制表示: "+Convert.ToString(166, 2));//十进制转八进制Console.Wri ...

  3. 为现有图像处理程序添加读写exif的功能

    为现有图像处理程序添加读取exif的功能 exif是图片的重要参数,在使用过程中很关键的一点是exif的数据能够和图片一起存在.exif的相关功能在操作系统中就集成了,在csharp中也似乎有了实现. ...

  4. c#进制转换(转)

    //十进制转二进制Console.WriteLine("十进制166的二进制表示: "+Convert.ToString(166, 2));//十进制转八进制Console.Wri ...

  5. Java API 快速速查宝典

    Java API 快速速查宝典 作者:明日科技,陈丹丹,李银龙,王国辉 著 出版社:人民邮电出版社 出版时间:2012年5月 Java编程的最基本要素是方法.属性和事件,掌握这些要素,就掌握了解决实际 ...

  6. C# 16进制与字符串、字节数组之间的转换(转)

    1.请问c#中如何将十进制数的字符串转化成十六进制数的字符串   //十进制转二进制 Console.WriteLine("十进制166的二进制表示: "+Convert.ToSt ...

  7. C# 实现屏幕键盘 (ScreenKeyboard)

    原文地址:http://www.cnblogs.com/youzai/archive/2008/05/19/1202732.html 要实现一个屏幕键盘,需要监听所有键盘事件,无论窗体是否被激活.因此 ...

  8. C#中ASCII码学习心得

    1.利用调用ASCIIEncoding类来实现各种转换.如简单个ACS码和int转换. ***利用(int)ASCIIEncoding类对象.GetBytes(character)[0]得到整数: p ...

  9. C# 最牛逼的Utility工具类

    完整代码: using System; using System.Collections.Specialized; using System.IO; using System.Net; using S ...

随机推荐

  1. div辅助线【完整版】

    ## <html> <head> <link rel="stylesheet" type="https://cdn.bootcss.com/ ...

  2. Centos 7 搭建.net web项目

    现在的.NET Core 1.0版本是一个很小的核心,APIs和工具也并不完整,但是随着.Net Core的不断完善,补充的Apis和创新也会一起整合到.NET Framework中. 安装cento ...

  3. 【python-HTMLTestRunner】生成HTMLTestRunner报告报错ERROR 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)

    [python-HTMLTestRunner]生成HTMLTestRunner报告报错:ERROR 'ascii' codec can't decode byte 0xe5 in position 0 ...

  4. kmp循环节

    循环判断   i%(i-next[i]) == 0 && next[i] != 0 循环长度  i-next[i];

  5. node.js使用redis储存session(详细步骤)

    转储session的原因 网上有许多session需要用数据库储存的原因,对我来说原因很简单,仅仅只是node的生产环境不允许将session存到服务器的内存中.会报一个内存溢出的风险警告.所以我决定 ...

  6. Windows10开发手记-Windows App Certification Kit使用教程

    最近有同学通过MVP的渠道询问我Windows 10应用提交审核前是否有工具能够像win7一样做App兼容性测试,其实Windows SDK已经继承了一套优秀的App测试认证工具,这可以帮助我们快速通 ...

  7. 678 "流浪地球"为什么是个好地方?(系统越复杂拥有好运气的机会也就越大)

    运气,其实就是一个复杂系统孕育出的,超出已知经验的解决方案.它不是没有产生机制.只不过,这个机制太复杂,涉及的因素太多.我们没法复制.所以,我们只能笼统的,把这套机制称为运气,或者命数. 举个例子,假 ...

  8. 内链接、左右连接、union并集

    第一个:内连接接 inner join select   *   from   a   inner   join   b   on   a.id=b.id where a.id =b.id (这种用法 ...

  9. sql 指定时间 所在的周、月、季、年

    DECLARE @TodayDateTime DATETIMEDECLARE @strToday NVARCHAR(19) DECLARE @TodayBeginDateTime DATETIMEDE ...

  10. welcome-file-list修改后不生效

    用别的浏览器重新尝试一下,或者清缓存.我就是这样解决的.值得注意的就是,<welcome-file>里面指定的文件可以是.do或者是action.