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 ...
随机推荐
- Paper | Batch Normalization
目录 1. PROBLEM 1.1. Introduction 1.2. Analysis 2. SOLUTION 2.1. Batch Normalization 及其问题 2.2. 梯度修正及其问 ...
- hive 命令行传入参数
azkban实现任务重跑 我们执行sql的方式是将hql文件上传到服务器本地.然后执行shell命令 hive " -f ./test_scheduler.hql 注:hive -e 是执行 ...
- Jquery 图片走马灯效果原理
本篇只讲解水平走马灯效果,垂直向上走马灯效果不讲解,原理一样,但是水平走马灯效果有一个小坑.待会讲解 照例先上代码: HTML: <div class="box"> & ...
- Javascript高级编程学习笔记(9)—— 执行环境
今天主要讲一下,JS底层的一些东西,这些东西不太好举例(应该是我水平不够) 望大家多多海涵,比心心 执行环境 执行环境(执行上下文,全文使用执行环境 )是JS中最为重要的一个概念,执行环境决定了,变量 ...
- 10.TreeSet、比较器
Comparable和Comparator Comparable 简介 Comparable 是排序接口.若一个类实现了Comparable接口,就意味着"该类支持排序". 即 ...
- HttpRunner Manager接口自动化测试平台实践(Windows)
1. 源码下载 github: https://github.com/HttpRunner/HttpRunnerManager 下载后放入项目目录,结构如下: 2.依赖环境 根据根目录require ...
- Telerik for AJAX RadGrid控件
作为一名.net小白,今天分享一下telerik知识的学习.熟悉ASP.NET Web Form的都知道Grid View或者是List View等表格控件,所以今天和大家分享一下telerik Ra ...
- Tools - 一些代码阅读的方法
1 初始能力 让阅读思路清晰连贯,保持在程序的流程架构和逻辑实现上,不被语法.编程技巧和业务流程等频繁地阻碍和打断. 语言基础:熟悉基础语法,常用的函数.库.编程技巧等: 了解设计模式.构建工具.代码 ...
- Spring MVC & Boot & Cloud 技术教程汇总(长期更新)
昨天我们发布了Java成神之路上的知识汇总,今天继续. Java成神之路技术整理(长期更新) 以下是Java技术栈微信公众号发布的关于 Spring/ Spring MVC/ Spring Boot/ ...
- Vue2.0+组件库总结
转自:https://blog.csdn.net/lishanleilixin/article/details/84025459 UI组件 element - 饿了么出品的Vue2的web UI工具套 ...