关于.NET(C#)中字符型(Char)与数字类型的转换, CLR via c# 读书笔记

 

原文:http://www.free56.cn/post/27.html

.NET中字符型(Char)与数字类型的转换 -- “CLR via C# ”读书笔记 (http://free56.cn)
三种实现方式,强制转换(Casting),使用System.Convert方法,使用IConvertible接口。其中,从效率来考虑,强制转换(Casting)效果最佳,因为在编译器编译时,会生成IL,而IL本身即可实现该转换,无需调用其他转换功能;使用IConvertible接口效果最差,因为实现时,还涉及到拆箱/装箱操作。

Convert between Char and various numeric types in .NET
(Afer read the "CLR via C#") (http://www.free56.cn)

There are 3 ways to convert between numertic types and Char instance, they are Casting, System.Convert method and IConvertible interface. I always use the Casting method, because it is  easy to use. And now ,after read the "CLR via C#", I know, that it is also the best and most efficient way to complete the convert function! The Casting Method as follows:
char c = (char) 65; // the char c is 'A'
int i = (int) 'A'; // the int i is 65

why it is the best way ? It is because the compiler emits intermediate language (IL) instructions to perform the conversion, and no methods have to be called. Interesting? So use it in this way!

The two other method... ... Please go to http://www.free56.cn/post/27.html

(http://www.free56.cn)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值