原文: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)