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. [Unity插件]DOTween基础

    官方文档链接:http://dotween.demigiant.com/documentation.PHP#globalSettings 普通版下载地址:http://dotween.demigian ...

  2. WPF 依赖属性&附加属性

    依赖属性 暂无 附加属性 1.在没有控件源码的前提下增加控件的属性 2.多个控件需要用到同一种属性 使用附加属性可以减少代码量,不必为每一个控件都增加依赖属性 3.属性不确定是否需要使用 在某些上下文 ...

  3. 方向导数,梯度和梯度下降之BGD,SGD

    方向导数和梯度的直观理解,from知乎-马同学: https://www.zhihu.com/question/36301367 BGD,SGD: https://www.cnblogs.com/gu ...

  4. PICE(5):MongoDBStreaming - gRPC -MGO Service

    我在前面提到过MongoDB不支持像SQL般字符式的操作指令,所以我们必须对所有的MongoDB操作指令建立protobuf类型才能支持MongoDB指令的序列化.在对上一篇博文里我们把MongoDB ...

  5. 从零开始的程序逆向之路 第一章——认识OD(Ollydbg)以及常用汇编扫盲

    作者:Crazyman_Army 原文来自:https://bbs.ichunqiu.com/thread-43041-1-1.html 0×00 序言: 1.自从上次笔者调戏完盗取文件密码大黑客后, ...

  6. spring框架学习笔记2:配置详解

    简单写一个类做示例: package bean; public class User { private String name; private Integer age; public String ...

  7. MANIFEST.MF文件详解

    1. 依赖包是否在classpath中: 2. 资源文件目录是否在classpath中: 3. 主类是否正确: 具体配置参考 maven-jar-plugin 配置 <plugin> &l ...

  8. 使用Swagger 搭建高可读性ASP.Net WebApi文档

    一.前言 在最近一个商城项目中,使用WebApi搭建API项目.但开发过程中,前后端工程师对于沟通接口的使用,是非常耗时的.之前也有用过Swagger构建WebApi文档,但是API文档的可读性并不高 ...

  9. Spring Boot 核心配置文件 bootstrap & application 详解。

    用过 Spring Boot 的都知道在 Spring Boot 中有以下两种配置文件 bootstrap (.yml 或者 .properties) application (.yml 或者 .pr ...

  10. [源码]Delphi源码免杀之函数动态调用 实现免杀的下载者

    [免杀]Delphi源码免杀之函数动态调用 实现免杀的下载者 2013-12-30 23:44:21         来源:K8拉登哥哥's Blog   自己编译这份代码看看 过N多杀软  没什么技 ...