<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>

            c#轉換關鍵詞explicit的使用

            時間:2025-11-26 21:56:06 C語言

            c#轉換關鍵詞explicit的使用

              引導語:C#適合為獨立和嵌入式的系統編寫程序,從使用復雜操作系統的大型系統到特定應用的小型系統均適用。以下是小編整理的c#轉換關鍵詞explicit的使用,歡迎參考閱讀!

              explicit 關鍵字用于聲明必須使用強制轉換來調用的用戶定義的類型轉換運算符。例如,在下面的示例中,此運算符將名為 Fahrenheit 的類轉換為名為 Celsius 的類:

              C#

              /pic/p>

              public static explicit operator Celsius(Fahrenheit f)

              {

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

              }

              可以如下所示調用此轉換運算符:

              C#

              Fahrenheit f = new Fahrenheit(100.0f);

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

              Celsius c = (Celsius)f;

              轉換運算符將源類型轉換為目標類型。源類型提供轉換運算符。與隱式轉換不同,必須通過強制轉換的方式來調用顯式轉換運算符。如果轉換操作可能導致異常或丟失信息,則應將其標記為 explicit。這可以防止編譯器無提示地調用可能產生無法預見后果的轉換操作。

              省略此強制轉換將導致編譯時錯誤 編譯器錯誤 CS0266。

              示例

              下面的示例提供 Fahrenheit 和 Celsius 類,它們中的每一個都為另一個提供顯式轉換運算符。

              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;

                }

                /pic/p>

                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

              */

              下面的示例定義一個結構 Digit,該結構表示單個十進制數字。定義了一個運算符,用于將 byte 轉換為 Digit,但因為并非所有字節都可以轉換為 Digit,所以該轉換是顯式的。

              C#

              struct Digit

              {

                byte value;

                public Digit(byte value)

                {

                  if (value > 9)

                  {

                    throw new ArgumentException();

                  }

                  this.value = value;

                }

                /pic/p>

                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; /pic/p>

                  }

                  catch (Exception e)

                  {

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

                  }

                }

              }

              /*

              Output:

              conversion occurred

              */

            【c#轉換關鍵詞explicit的使用】相關文章:

            c#訪問關鍵詞base的使用11-29

            c#檢測cpu使用率01-02

            c#中預處理指令#if的使用02-01

            c#中訪問關鍵詞 this 的常用用途09-23

            c#中預處理指令#line的使用11-12

            c#查詢關鍵字之into的使用02-04

            c#運算符關鍵字is的使用02-03

            c#查詢關鍵字之group子句的使用02-24

            php下intval()和(int)轉換使用02-05

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看