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);
        }
    }
}
/*
輸出:
轉換已完成
*/

參考資料


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM