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 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的更多相关文章
- 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 ...
- 进制转换及API接口中的转换
//十进制转二进制Console.WriteLine("十进制166的二进制表示: "+Convert.ToString(166, 2));//十进制转八进制Console.Wri ...
- 为现有图像处理程序添加读写exif的功能
为现有图像处理程序添加读取exif的功能 exif是图片的重要参数,在使用过程中很关键的一点是exif的数据能够和图片一起存在.exif的相关功能在操作系统中就集成了,在csharp中也似乎有了实现. ...
- c#进制转换(转)
//十进制转二进制Console.WriteLine("十进制166的二进制表示: "+Convert.ToString(166, 2));//十进制转八进制Console.Wri ...
- Java API 快速速查宝典
Java API 快速速查宝典 作者:明日科技,陈丹丹,李银龙,王国辉 著 出版社:人民邮电出版社 出版时间:2012年5月 Java编程的最基本要素是方法.属性和事件,掌握这些要素,就掌握了解决实际 ...
- C# 16进制与字符串、字节数组之间的转换(转)
1.请问c#中如何将十进制数的字符串转化成十六进制数的字符串 //十进制转二进制 Console.WriteLine("十进制166的二进制表示: "+Convert.ToSt ...
- C# 实现屏幕键盘 (ScreenKeyboard)
原文地址:http://www.cnblogs.com/youzai/archive/2008/05/19/1202732.html 要实现一个屏幕键盘,需要监听所有键盘事件,无论窗体是否被激活.因此 ...
- C#中ASCII码学习心得
1.利用调用ASCIIEncoding类来实现各种转换.如简单个ACS码和int转换. ***利用(int)ASCIIEncoding类对象.GetBytes(character)[0]得到整数: p ...
- C# 最牛逼的Utility工具类
完整代码: using System; using System.Collections.Specialized; using System.IO; using System.Net; using S ...
随机推荐
- Django——用户认证和判断用户是否登录
用户认证 必须通过认证之后才能login(request,user)这样才能保存会话到request中,注销后会话结束 注意 自定义的用户登陆时只不止需要验证用户名和密码的需要写认证,就例如在线教育平 ...
- No趴笨小分队
这星期完成了小组的取名这一项重大的活动. 正所谓“名字是一个好开头”,取这个名义有以下的意义: 希望之后的学习以及工作能一帆风顺: 祝福各位小组成员之后的路能顺顺利利: 希望能在组员磨合的过程中可以愉 ...
- Pycharm的激活码,亲测可用(20181223)
K03CHKJCFT-eyJsaWNlbnNlSWQiOiJLMDNDSEtKQ0ZUIiwibGljZW5zZWVOYW1lIjoibnNzIDEwMDEiLCJhc3NpZ25lZU5hbWUiO ...
- 【接口时序】2、Verilog实现流水灯及与C语言的对比
一. 软件平台与硬件平台 软件平台: 1.操作系统:Windows-8.1 2.开发套件:ISE14.7 3.仿真工具:ModelSim-10.4-SE 硬件平台: 1.FPGA型号:XC6SLX45 ...
- 读入挂(IO)
快如闪电,清华杜瑜皓的读入挂,一模一样代码,加了这个之后... 细思极恐,and 整整行!!! namespace IO{ #define BUF_SIZE 100000 #define OUT_SI ...
- 利用Burp Suite攻击Web应用
i春秋作家:Passerby2 web应用测试综述: Web应用漏洞给企业信息系统造成了很大的风险.许多web应用程序漏洞是由于web应用程序缺乏对输入的过滤.简而言之Web应用程序利用来自用户的某种 ...
- scikit-image 图像处理库介绍
今天学习图像处理的时候,无意中看到了scikit 软件包,抱着学习的态度,这里做个记录,方便以后翻阅. 概念:scikit-image 是一种开源的用于图像处理的 Python 包.它包括分割,几何变 ...
- Pycharm中自动生成作者,日期等信息
初次安装使用PyCharm,在新建.py文件时会发现文件头并没有什么信息,因此,使用模板会比较方便.方法如下: 1.打开PyCharm,选择File--Settings 2.依次选择Editor--- ...
- 【深入 MongoDB 开发】使用正确的姿势连接分片集群
MongoDB分片集群(Sharded Cluster)通过将数据分散存储到多个分片(Shard)上,来实现高可扩展性.实现分片集群时,MongoDB 引入 Config Server 来存储集群的元 ...
- Win10上安装TensorFlow(官方文档翻译)
一.推荐两个网站 TensorFlow官方文档:https://www.tensorflow.org/install/install_windows TensorFlow中文社区:http://www ...