學習C#中的類型轉換

來源:互聯網
上載者:User
 

C# 出來也有些日子了,最近由於編程的需要,對 C# 的類型轉換做了一些研究,其內容涉及 C# 的裝箱/拆箱/別名、數實值型別間相互轉換、字元的 ASCII 碼和 Unicode 碼、數值字串和數值之間的轉換、字串和字元數組/位元組數組之間的轉換、各種數實值型別和位元組數組之間的轉換、十六進位數輸出以及日期型資料的一些轉換處理,在這裡與大家分享—— 1. 裝箱、拆箱還是別名。   

許多 C#.NET 的書上都有介紹 int -> Int32 是一個裝箱的過程,反之則是拆箱的過程。許多其它變數類型也是如此,如:short <-> Int16,long <-> Int64 等。對於一般的程式員來說,大可不必去瞭解這一過程,因為這些裝箱和拆箱的動作都是可以自動完成的,不需要寫代碼進行幹預。但是我們需要記住這些類型之間的關係,所以,我們使用“別名”來記憶它們之間的關係。 C# 是全物件導向的語言,比 Java 的物件導向都還徹底——它把單一資料型別通過預設的裝箱動作封裝成了類。Int32、Int16、Int64 等就是相應的類名,而那些我們熟悉的、簡單易記的名稱,如 int、short、long 等,我們就可以把它稱作是 Int32、Int16、Int64 等類型的別名。  

那麼除了這三種類型之外,還有哪些類有“別名”呢?

常用的有如下一些: bool -> System.Boolean (布爾型,其值為 true 或者 false) char -> System.Char (字元型,佔有兩個位元組,表示 1 個 Unicode 字元) byte -> System.Byte (位元組型,占 1 位元組,表示 8 位正整數,範圍 0 ~ 255) sbyte -> System.SByte (帶符號位元組型,占 1 位元組,表示 8 位整數,範圍 -128 ~ 127) ushort -> System.UInt16 (無符號短整型,占 2 位元組,表示 16 位正整數,範圍 0 ~ 65,535) uint -> System.UInt32 (無符號整型,占 4 位元組,表示 32 位正整數,範圍 0 ~ 4,294,967,295) ulong -> System.UInt64 (無符號長整型,占 8 位元組,表示 64 位元正整數,範圍 0 ~ 大約 10 的 20 次方) short -> System.Int16 (短整型,占 2 位元組,表示 16 位整數,範圍 -32,768 ~ 32,767) int -> System.Int32 (整型,占 4 位元組,表示 32 位整數,範圍 -2,147,483,648 到 2,147,483,647) long -> System.Int64 (長整型,占 8 位元組,表示 64 位元整數,範圍大約 -(10 的 19) 次方 到 10 的 19 次方) float -> System.Single (單精確度浮點型,占 4 個位元組) double -> System.Double (雙精確度浮點型,占 8 個位元組)。

我們可以用下列代碼做一個實驗:

private void TestAlias()

{

// this.textBox1 是一個文字框,類型為 System.Windows.Forms.TextBox

// 設計中已經將其 Multiline 屬性設定為

true byte a = 1;

char b = a;

short c = 1;

int d = 2;

long e = 3;

uint f = 4;

bool g = true;

this.textBox1.Text = "";

this.textBox1.AppendText("byte -> " + a.GetType().FullName + "\n"); this.textBox1.AppendText("char -> " + b.GetType().FullName + "\n"); this.textBox1.AppendText("short -> " + c.GetType().FullName + "\n"); this.textBox1.AppendText("int -> " + d.GetType().FullName + "\n"); this.textBox1.AppendText("long -> " + e.GetType().FullName + "\n"); this.textBox1.AppendText("uint -> " + f.GetType().FullName + "\n"); this.textBox1.AppendText("bool -> " + g.GetType().FullName + "\n");

}   

在表單中建立一個按鈕,並在它的單擊事件中調用該 TestAlias() 函數,我們將看到運行結果如下: byte -> System.Byte char -> System.Char short -> System.Int16 int -> System.Int32 long -> System.Int64 uint -> System.UInt32 bool -> System.Boolean   這足以說明各別名對應的類!

2. 數實值型別之間的相互轉換,這裡所說的數實值型別包括 byte, short, int, long, fload, double 等,根據這個排列順序,各種類型的值依次可以向後自動進行轉換。舉個例來說,把一個 short 型的資料賦值給一個 int 型的變數,short 值會自動行轉換成 int 型值,再賦給 int 型變數。如下例:

private void TestBasic()

{

byte a = 1;

short b = a;

int c = b;

long d = c;

float e = d;

double f = e;

this.textBox1.Text = "";

this.textBox1.AppendText("byte a = " + a.ToString() + "\n"); this.textBox1.AppendText("short b = " + b.ToString() + "\n"); this.textBox1.AppendText("int c = " + c.ToString() + "\n"); this.textBox1.AppendText("long d = " + d.ToString() + "\n"); this.textBox1.AppendText("float e = " + e.ToString() + "\n"); this.textBox1.AppendText("double f = " + f.ToString() + "\n");

}   

編譯順利通過,運行結果是各變數的值均為 1;當然,它們的類型分別還是 System.Byte 型……System.Double 型。現在我們來試試,如果把賦值的順序反過來會怎麼樣呢?在 TestBasic() 函數中追加如下語句: int g = 1; short h = g; this.textBox1.AppendText("h = " + h.ToString() + "\n");   結果編譯報錯G:\Projects\Visual C#\Convert\Form1.cs(118): 無法將類型“int”隱式轉換為“short”   其中,Form1.cs 的 118 行即 short h = g 所在行。   這個時候,如果我們堅持要進行轉換,就應該使用強制類型轉換,這在 C 語言中常有提及,就是使用“(類型名) 變數名”形式的語句來對資料進行強制轉換。如上例修改如下:

 short g = 1; byte h = (byte) g;

// 將 short 型的 g 的值強制轉換成 short 型後再賦給變數 h this.textBox1.AppendText("h = " + h.ToString() + "\n");   

編譯通過,運行結果輸出了 h = 1,轉換成功。   但是,如果我們使用強制轉換,就不得不再考慮一個問題:short 型的範圍是 -32768 ~ 23767,而 byte 型的範圍是 0 ~ 255,那麼,如果變數 g 的大小超過了 byte 型的範圍又會出現什麼樣的情況呢?我們不妨再一次改寫代碼,將值改為 265,比 255 大 10 short g = 265; //265 = 255 + 10 byte h = (byte) g; this.textBox1.AppendText("h = " + h.ToString() + "\n");   編譯沒有出錯,運行結果卻不是 h = 265,而是 h = 9。 因此,我們在進行轉換的時候,應當注意被轉換的資料不能超出目標類型的範圍。這不僅體現在多位元組資料類型(相對,如上例的 short) 轉換為少位元組類型(相對,如上例的 byte) 時,也體現在位元組數相同的有符號類型和無符號類型之間,如將 byte 的 129 轉換為 sbyte 就會溢出。這方面的例子大同小異,就不詳細說明了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.