1. 程式人生 > >C#中的explicit和implicit瞭解一下吧

C#中的explicit和implicit瞭解一下吧

今天在研究公司專案框架的時候看到了下面的用法,public static implicit operator JsonData(int data);。貌似很久沒用過這種隱士轉換的寫法了,因此重新溫習一下C#中轉換相關的知識。

作者:依樂祝
原文地址:https://www.cnblogs.com/yilezhu/p/10898582.html

implicit

implicit 關鍵字用於宣告隱式的使用者自定義的型別轉換運算子。 如果可以確保轉換過程不會造成資料丟失,則可使用該關鍵字在使用者定義型別和其他型別之間進行隱式轉換。

使用隱式轉換操作符之後,在編譯時會跳過異常檢查,所以隱式轉換運算子應當從不引發異常並且從不丟失資訊,否則在執行時會出現一些意想不到的問題。

示例

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();
    }
}

隱式轉換可以通過消除不必要的強制轉換來提高原始碼的可讀性。 但是,因為隱式轉換不需要程式設計師將一種型別顯式強制轉換為另一種型別,所以使用隱式轉換時必須格外小心,以免出現意外結果。 一般情況下,隱式轉換運算子應當從不引發異常並且從不丟失資訊,以便可以在程式設計師不知曉的情況下安全使用它們。 如果轉換運算子不能滿足那些條件,則應將其標記為 explicit。 有關詳細資訊,請參閱使用轉換運算子。

explicit顯示轉換

explicit 關鍵字宣告必須通過顯示的呼叫使用者定義的型別轉換運算子來進行轉換。

以下示例定義從 Fahrenheit 類轉換為 Celsius 類的運算子。 必須在 Fahrenheit

類或 Celsius 類中定義運算子:

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

如下所示,呼叫使用者定義的轉換運算子來強制轉換:

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

此轉換運算子從源型別轉換為目標型別。 源型別提供轉換運算子。 不同於隱式轉換,顯式轉換運算子必須通過轉換的方式來呼叫。 如果轉換操作會導致異常或丟失資訊,則應將其標記為 explicit。 這可阻止編譯器靜默呼叫可能產生意外後果的轉換操作。

省略轉換將導致編譯時錯誤 CS0266。

有關詳細資訊,請參閱使用轉換運算子。

示例

下面的示例提供了 FahrenheitCelsius 類,其中每個類均提供轉換為其他類的顯式轉換運算子。

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

示例

下面的示例定義結構 Digit,它表示單個的十進位制數字。 將運算子定義為從 byteDigit 的轉換,但由於並非所有位元組都可轉換為 Digit,因此該轉換應該應用顯式轉換。

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

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

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

參考資料

  • explicit
  • operator (C# Reference)
  • How to: Implement User-Defined Conversions Between Structs
  • implicit