關於.NET(C#)中字元型(Char)與數字類型的轉換, CLR via c# 讀書筆記

來源:互聯網
上載者:User

 

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.