溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#中explicit與implicit的區(qū)別是什么

發(fā)布時(shí)間:2021-06-11 17:16:39 來源:億速云 閱讀:218 作者:Leah 欄目:編程語言

C#中explicit與implicit的區(qū)別是什么,相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

implicit

implicit 關(guān)鍵字用于聲明隱式的用戶自定義的類型轉(zhuǎn)換運(yùn)算符。 如果可以確保轉(zhuǎn)換過程不會(huì)造成數(shù)據(jù)丟失,則可使用該關(guān)鍵字在用戶定義類型和其他類型之間進(jìn)行隱式轉(zhuǎn)換。

使用隱式轉(zhuǎn)換操作符之后,在編譯時(shí)會(huì)跳過異常檢查,所以隱式轉(zhuǎn)換運(yùn)算符應(yīng)當(dāng)從不引發(fā)異常并且從不丟失信息,否則在運(yùn)行時(shí)會(huì)出現(xiàn)一些意想不到的問題。

示例

class Digit
{
 public Digit(double d) { val = d; }
 public double val;
 // ...other members

 // User-defined conversion from Digit to double
 public static implicit operator double(Digit d)
 {
  return d.val;
 }
 // User-defined conversion from double to Digit
 public static implicit operator Digit(double d)
 {
  return new Digit(d);
 }
}

class Program
{
 static void Main(string[] args)
 {
  Digit dig = new Digit(7);
  //This call invokes the implicit "double" operator
  double num = dig;
  //This call invokes the implicit "Digit" operator
  Digit dig2 = 12;
  Console.WriteLine("num = {0} dig2 = {1}", num, dig2.val);
  Console.ReadLine();
 }
}

隱式轉(zhuǎn)換可以通過消除不必要的強(qiáng)制轉(zhuǎn)換來提高源代碼的可讀性。 但是,因?yàn)殡[式轉(zhuǎn)換不需要程序員將一種類型顯式強(qiáng)制轉(zhuǎn)換為另一種類型,所以使用隱式轉(zhuǎn)換時(shí)必須格外小心,以免出現(xiàn)意外結(jié)果。 一般情況下,隱式轉(zhuǎn)換運(yùn)算符應(yīng)當(dāng)從不引發(fā)異常并且從不丟失信息,以便可以在程序員不知曉的情況下安全使用它們。 如果轉(zhuǎn)換運(yùn)算符不能滿足那些條件,則應(yīng)將其標(biāo)記為 explicit。 有關(guān)詳細(xì)信息,請參閱使用轉(zhuǎn)換運(yùn)算符。

explicit顯示轉(zhuǎn)換

explicit 關(guān)鍵字聲明必須通過顯示的調(diào)用用戶定義的類型轉(zhuǎn)換運(yùn)算符來進(jìn)行轉(zhuǎn)換。

以下示例定義從 Fahrenheit 類轉(zhuǎn)換為 Celsius 類的運(yùn)算符。 必須在 Fahrenheit 類或 Celsius 類中定義運(yùn)算符:

public static explicit operator Celsius(Fahrenheit fahr)
{
 return new Celsius((5.0f / 9.0f) * (fahr.Degrees - 32));
}

如下所示,調(diào)用用戶定義的轉(zhuǎn)換運(yùn)算符來強(qiáng)制轉(zhuǎn)換:

Fahrenheit fahr = new Fahrenheit(100.0f);
Console.Write($"{fahr.Degrees} Fahrenheit");
Celsius c = (Celsius)fahr;

此轉(zhuǎn)換運(yùn)算符從源類型轉(zhuǎn)換為目標(biāo)類型。 源類型提供轉(zhuǎn)換運(yùn)算符。 不同于隱式轉(zhuǎn)換,顯式轉(zhuǎn)換運(yùn)算符必須通過轉(zhuǎn)換的方式來調(diào)用。 如果轉(zhuǎn)換操作會(huì)導(dǎo)致異?;騺G失信息,則應(yīng)將其標(biāo)記為 explicit。 這可阻止編譯器靜默調(diào)用可能產(chǎn)生意外后果的轉(zhuǎn)換操作。

省略轉(zhuǎn)換將導(dǎo)致編譯時(shí)錯(cuò)誤 CS0266。

有關(guān)詳細(xì)信息,請參閱使用轉(zhuǎn)換運(yùn)算符。

示例

下面的示例提供了 Fahrenheit 和 Celsius 類,其中每個(gè)類均提供轉(zhuǎn)換為其他類的顯式轉(zhuǎn)換運(yùn)算符。

class Celsius
{
 public Celsius(float temp)
 {
  Degrees = temp;
 }
 
 public float Degrees { get; }
 
 public static explicit operator Fahrenheit(Celsius c)
 {
  return new Fahrenheit((9.0f / 5.0f) * c.Degrees + 32);
 }
}

class Fahrenheit
{
 public Fahrenheit(float temp)
 {
  Degrees = temp;
 }
 
 public float Degrees { get; }
 
 public static explicit operator Celsius(Fahrenheit fahr)
 {
  return new Celsius((5.0f / 9.0f) * (fahr.Degrees - 32));
 }
}

class MainClass
{
 static void Main()
 {
  Fahrenheit fahr = new Fahrenheit(100.0f);
  Console.Write($"{fahr.Degrees} Fahrenheit");
  Celsius c = (Celsius)fahr;

  Console.Write($" = {c.Degrees} Celsius");
  Fahrenheit fahr2 = (Fahrenheit)c;
  Console.WriteLine($" = {fahr2.Degrees} Fahrenheit");
 }
}
// 輸出:
// 100 Fahrenheit = 37.77778 Celsius = 100 Fahrenheit

示例

下面的示例定義結(jié)構(gòu) Digit,它表示單個(gè)的十進(jìn)制數(shù)字。 將運(yùn)算符定義為從 byte 到 Digit 的轉(zhuǎn)換,但由于并非所有字節(jié)都可轉(zhuǎn)換為 Digit,因此該轉(zhuǎn)換應(yīng)該應(yīng)用顯式轉(zhuǎn)換。

struct Digit
{
 byte value;
 public Digit(byte value)
 {
  if (value > 9)
  {
   throw new ArgumentException();
  }
  this.value = value;
 }

 // 定義從byte到Digit的顯示轉(zhuǎn)換 explicit operator:
 public static explicit operator Digit(byte b)
 {
  Digit d = new Digit(b);
  Console.WriteLine("轉(zhuǎn)換已完成");
  return d;
 }
}

class ExplicitTest
{
 static void Main()
 {
  try
  {
   byte b = 3;
   Digit d = (Digit)b; // 顯示轉(zhuǎn)換
  }
  catch (Exception e)
  {
   Console.WriteLine("{0} 捕獲到一成.", e);
  }
 }
}
/*
輸出:
轉(zhuǎn)換已完成
*/

看完上述內(nèi)容,你們掌握C#中explicit與implicit的區(qū)別是什么的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI