欧美日韩不卡一区二区三区,www.蜜臀.com,高清国产一区二区三区四区五区,欧美日韩三级视频,欧美性综合,精品国产91久久久久久,99a精品视频在线观看

C語言

c#轉(zhuǎn)換關(guān)鍵詞explicit的使用

時(shí)間:2025-03-27 23:31:01 C語言 我要投稿
  • 相關(guān)推薦

c#轉(zhuǎn)換關(guān)鍵詞explicit的使用

  引導(dǎo)語:C#適合為獨(dú)立和嵌入式的系統(tǒng)編寫程序,從使用復(fù)雜操作系統(tǒng)的大型系統(tǒng)到特定應(yīng)用的小型系統(tǒng)均適用。以下是小編整理的c#轉(zhuǎn)換關(guān)鍵詞explicit的使用,歡迎參考閱讀!

  explicit 關(guān)鍵字用于聲明必須使用強(qiáng)制轉(zhuǎn)換來調(diào)用的用戶定義的類型轉(zhuǎn)換運(yùn)算符。例如,在下面的示例中,此運(yùn)算符將名為 Fahrenheit 的類轉(zhuǎn)換為名為 Celsius 的類:

  C#

  // Must be defined inside a class called Farenheit:

  public static explicit operator Celsius(Fahrenheit f)

  {

    return new Celsius((5.0f / 9.0f) * (f.degrees - 32));

  }

  可以如下所示調(diào)用此轉(zhuǎn)換運(yùn)算符:

  C#

  Fahrenheit f = new Fahrenheit(100.0f);

  Console.Write("{0} fahrenheit", f.Degrees);

  Celsius c = (Celsius)f;

  轉(zhuǎn)換運(yùn)算符將源類型轉(zhuǎn)換為目標(biāo)類型。源類型提供轉(zhuǎn)換運(yùn)算符。與隱式轉(zhuǎn)換不同,必須通過強(qiáng)制轉(zhuǎn)換的方式來調(diào)用顯式轉(zhuǎn)換運(yùn)算符。如果轉(zhuǎn)換操作可能導(dǎo)致異;騺G失信息,則應(yīng)將其標(biāo)記為 explicit。這可以防止編譯器無提示地調(diào)用可能產(chǎn)生無法預(yù)見后果的轉(zhuǎn)換操作。

  省略此強(qiáng)制轉(zhuǎn)換將導(dǎo)致編譯時(shí)錯(cuò)誤 編譯器錯(cuò)誤 CS0266。

  示例

  下面的示例提供 Fahrenheit 和 Celsius 類,它們中的每一個(gè)都為另一個(gè)提供顯式轉(zhuǎn)換運(yùn)算符。

  C#

  class Celsius

  {

    public Celsius(float temp)

    {

      degrees = temp;

    }

    public static explicit operator Fahrenheit(Celsius c)

    {

      return new Fahrenheit((9.0f / 5.0f) * c.degrees + 32);

    }

    public float Degrees

    {

      get { return degrees; }

    }

    private float degrees;

  }

  class Fahrenheit

  {

    public Fahrenheit(float temp)

    {

      degrees = temp;

    }

    // Must be defined inside a class called Farenheit:

    public static explicit operator Celsius(Fahrenheit f)

    {

      return new Celsius((5.0f / 9.0f) * (f.degrees - 32));

    }

    public float Degrees

    {

      get { return degrees; }

    }

    private float degrees;

  }

  class MainClass

  {

    static void Main()

    {

      Fahrenheit f = new Fahrenheit(100.0f);

      Console.Write("{0} fahrenheit", f.Degrees);

      Celsius c = (Celsius)f;

      Console.Write(" = {0} celsius", c.Degrees);

      Fahrenheit f2 = (Fahrenheit)c;

      Console.WriteLine(" = {0} fahrenheit", f2.Degrees);

    }

  }

  /*

  Output:

  100 fahrenheit = 37.77778 celsius = 100 fahrenheit

  */

  下面的示例定義一個(gè)結(jié)構(gòu) Digit,該結(jié)構(gòu)表示單個(gè)十進(jìn)制數(shù)字。定義了一個(gè)運(yùn)算符,用于將 byte 轉(zhuǎn)換為 Digit,但因?yàn)椴⒎撬凶止?jié)都可以轉(zhuǎn)換為 Digit,所以該轉(zhuǎn)換是顯式的。

  C#

  struct Digit

  {

    byte value;

    public Digit(byte value)

    {

      if (value > 9)

      {

        throw new ArgumentException();

      }

      this.value = value;

    }

    // Define explicit byte-to-Digit conversion operator:

    public static explicit operator Digit(byte b)

    {

      Digit d = new Digit(b);

      Console.WriteLine("conversion occurred");

      return d;

    }

  }

  class ExplicitTest

  {

    static void Main()

    {

      try

      {

        byte b = 3;

        Digit d = (Digit)b; // explicit conversion

      }

      catch (Exception e)

      {

        Console.WriteLine("{0} Exception caught.", e);

      }

    }

  }

  /*

  Output:

  conversion occurred

  */

【c#轉(zhuǎn)換關(guān)鍵詞explicit的使用】相關(guān)文章:

c#訪問關(guān)鍵詞base的使用10-02

c#中預(yù)處理指令#if的使用08-18

c#檢測(cè)cpu使用率09-01

c#中訪問關(guān)鍵詞 this 的常用用途08-27

c#中預(yù)處理指令#line的使用05-20

c#查詢關(guān)鍵字之into的使用07-25

c#運(yùn)算符關(guān)鍵字is的使用10-30

c#查詢關(guān)鍵字之group子句的使用09-07

php下intval()和(int)轉(zhuǎn)換使用06-02