C# 中的型別轉換

王林
發布: 2024-09-03 15:07:27
原創
993 人瀏覽過

C# 中的類型轉換定義為我們將任何資料類型指派給另一種資料類型,然後將其稱為「類型轉換」。任何程式語言的較低資料類型值都可以自動轉換為較高資料類型值。在這種情況下,不會遺失數據,而在將較高資料類型值轉換為較低資料類型值的情況下,可能會遺失資料。低層資料型別到高層資料型別可以由 C# 編譯器自動完成,但是高層資料型別到低階資料的型別轉換,我們必須明確型別轉換。這就是所謂的「顯性強制轉換」。

讓我們舉個例子,將 long 值轉換為 int 值是明確型別轉換。

為什麼明確轉換需要較大的資料型別到較小的資料型別?

打個小比方就明白了,有2個水瓶,一個是1公升,一個是2公升。我們可以輕鬆地將1公升水倒入2公升水瓶中而不會溢出。同樣的道理,如果我們嘗試將 2 公升水瓶中的水倒入 1 公升水中,那麼如果 2 公升水瓶中的水超過 1 公升,水可能會溢出。因此,在本例中,1 公升水瓶是較低的資料類型,2 公升水瓶是較高的資料類型。即使有機會溢出水,我們仍然想將2公升水倒入1公升水瓶中,我們可以倒這樣客戶必須接受這樣做。同樣,開發人員有明確的想法,即使我們嘗試將較高的數據類型轉換為較低的數據類型,也可能會丟失數據,所以他必須接受它。

C# 中的型別轉換

C# 中有 2 種類型的轉換。

  • 明確型別轉換
  • 隱式型別轉換:較小資料型別到較大資料型別的轉換稱為「隱式型別轉換」。這是由 C# 編譯器自動完成的。不會遺失資料。

1.隱式型別轉換

位元組->短->整數->長->浮點->雙精確度

代碼:

Bigger_dataType  variableName=smaller_dataType_Value;
登入後複製

2.明確型別轉換

較大資料型別到較小資料型別的轉換稱為「顯式型別轉換」。這不是由 C# 編譯器自動完成的。可能會遺失資料。這必須由開發人員明確完成。

位元組->短、整數、長、浮點、雙精確度

短->int、long、float、double

int->long、float、double

長->浮動,雙

浮動->雙

代碼:

Smaller_dataType  variableName=(Smaller_dataType)Bigger_dataType_Value;
登入後複製
注意: 轉換僅適用於相容的資料類型,意味著僅進行數字轉換的數字,而不是具有數字的字串,反之亦然,等等。我這樣做可能會發生不支援的異常。

型別轉換的方法

  • ToBoolean:它將型別轉換為布林值。
  • ToChar:它將類型轉換為字元值。
  • ToByte:它將一個值轉換為位元組值。
  • ToDecimal:它將一個值轉換為小數點值。
  • ToDouble:它將類型轉換為雙精確度資料類型。
  • ToInt16:它將型別轉換為 16 位元整數
  • ToInt32:它將型別轉換為 32 位元整數
  • ToInt64:它將型別轉換為 64 位元整數
  • ToString:它將給定類型轉換為 String
  • ToUInt16:它將型別轉換為無符號 16 位元整數
  • ToUInt32:它將型別轉換為無符號 32 位元整數
  • ToUInt64:它將型別轉換為無符號 64 位元整數

在 C# 中實作型別轉換的範例

以下是一些例子:

範例#1

隱式型別轉換

代碼:

//including System package in C#
using System;
//Creating class
public class ImplicitTypeCasting {
// main method for application to run
public static void Main(String []args)
{
//variable declaration and initialization
int intType = 200;
// Implicit int to long casting
long longType = intType;
// Implicit long to float casting
float floatType = longType;
// Printing output of implicit conversion variables
Console.WriteLine("INT value after Implicit conversion: " +intType);
Console.WriteLine("LONG value after Implicit conversion:" +longType);
Console.WriteLine("FLOAT value after Implicit conversion: " +floatType);
}
}
登入後複製

輸出:

C# 中的型別轉換

範例#2

嘗試從較大的型別到較低的型別隱式型別轉換

代碼:

//including System package in C#
using System;
//Creating class
public class ImplicitCastingBiggerToSmaller {
// main method for application to run
public static void Main(String []args)
{
//variable declaration and initialization
int intType = 200;
// Trying to convert int to byte Implicitly but there is compile time error
byte byteType = intType;
// Trying to convert int to short Implicitly but there is compile time error
short shortType = intType;
// Printing output of implicit conversion variables
Console.WriteLine("INT value after Implicit conversion: " +intType);
Console.WriteLine("BYTE value after Implicit conversion:" +byteType);
Console.WriteLine("SHORT value after Implicit conversion: " +shortType);
}
}
登入後複製

輸出:

C# 中的型別轉換

範例#3

明確型別轉換

代碼:

//including System package in C#
using System;
//Creating class
public class ExplicitCastingBiggerToSmaller {
// main method for application to run
public static void Main(String []args)
{
//variable declaration and initialization
int intType = 9999999;
int intType1=120;
// Trying to convert int to byte explicitly
byte byteType = (byte)intType;
byte byteType1 = (byte)intType1;
// Trying to convert int to short explicitly
short shortType = (short)intType;
short shortType1 = (short)intType1;
// Printing output of explicit conversion variables
//Given int range is not in byte and short range so there must be loss of data result into incorrect output
Console.WriteLine("BYTE value after Explicit conversion: " +byteType);
Console.WriteLine("SHORT value after Explicit conversion: " +shortType);
Console.WriteLine("\n");
// Printing output of explicit conversion variables
//Given int range is in byte and short range so there no data loss
Console.WriteLine("BYTE value after Explicit conversion: " +byteType1);
Console.WriteLine("SHORT value after Explicit conversion: " +shortType1);
}
}<strong> </strong>
登入後複製

輸出:

C# 中的型別轉換

範例#4

使用預先定義方法進行明確型別轉換

代碼:

//including System package in C#
using System;
//Creating class
public class ExplicitCastingBiggerToSmaller {
// main method for application to run
public static void Main(String []args)
{
//variable declaration and initialization
int intType = 9999999;
double doubleType=122.23;
float floatType=222.222f;
// Printing output of explicit convertion variables
//By using C# predefined method for type casting
Console.WriteLine("INT to STRING type value with predefined method convertion: "+Convert.ToString(intType));
Console.WriteLine("DOUBLE to INT type value with predefined method convertion: "+Convert.ToInt32(doubleType));
Console.WriteLine("FLOAT to INT type value with predefined method convertion: "+Convert.ToUInt32(floatType));
}
}
登入後複製

輸出:

C# 中的型別轉換

結論

C# 有兩種類型的型別轉換,第一種st 一種是隱式型別轉換,第二種是顯式型別轉換。隱式類型轉換由編譯器自動完成,但明確型別轉換開發人員必須執行,因為在這種情況下可能會遺失資料。

以上是C# 中的型別轉換的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!