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. python之路(六)-函数相关

    在没有学习函数之前我们的程序是面向过程的,不停的判断,不停的循环,同样的代码重复出现在我们的代码里.函数可以更好的提高我们的 代码质量,避免同样的代码重复出现,而只需要在用的时候调用函数即可执行.此为 ...

  2. 【UWP】使用 Rx 改善 AutoSuggestBox

    在 UWP 中,有一个控件叫 AutoSuggestBox,它的主要成分是一个 TextBox 和 ComboBox.使用它,我们可以做一些根据用户输入来显示相关建议输入的功能,例如百度首页搜索框那种 ...

  3. .net core mvc发布项目到IIS上出现500错误

    如题,我把.net core mvc项目以应用程序方式挂到IIS默认网站下,结果出现了如下错误:HTTP Error 500.0 - ANCM In-Process Handler Load Fail ...

  4. grid布局笔记学习一之父元素(容器)

    HTML代码: <div id="box"> <div class="lbox box1" style="background: # ...

  5. web开发之菜鸟的代码规范

    笔者菜鸟里最不会飞的那个,所以这些基础的习惯都没养成,正好抽时间特意做个笔记以方便自己规范代码, 有兴趣的大佬多多指点. 养成好的编码习惯收益良多, 总结下编码时应注意的细节<借鉴高程里代码约束 ...

  6. JSON 字符串解析技巧总结

    在解析JSONObject的字符数据的时候,可以考虑去使用optString 解析网络JSON数据时,获取数据的两个方法optString和getString: 使用optString获取数据时,即使 ...

  7. kubernetes集群搭建(9):docker 镜像的导入与导出

    由于K8s搭建时官方提供的镜像不FQ是访问不了的,所以搭建过程中很多时间都耗费在去找镜像去了 下面是我搭建k8s集群用到的镜像  没有采用之前我用的二进制文件搭建(dns没成功),这里采用的Kubea ...

  8. Pycharm中自动生成作者,日期等信息

    初次安装使用PyCharm,在新建.py文件时会发现文件头并没有什么信息,因此,使用模板会比较方便.方法如下: 1.打开PyCharm,选择File--Settings 2.依次选择Editor--- ...

  9. iOS开发总结——项目目录结构

    1.前言 清晰的项目目录结构有利于项目的开发,同时也是软件架构的一部分,所以,项目开发之初搭建项目的目录结构很重要.刚转iOS时,自己并不知道如何搭建App的项目目录,在参与开发两个应用后,结合Web ...

  10. 使用Project进行项目管理

    下面开始介绍Project的使用. 1.  从下列地址获取Project 2010的副本. 版权问题,已删除地址. 2.安装 2.1 版权页 2.2 自定义安装页 2.3 安装完毕. 3.使用该软件进 ...