关于.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)

posted on 2007-04-19 09:47  Jack Niu  阅读(663)  评论(0编辑  收藏  举报

Affiliate Marketing and Web Technology