转换关键字(C# 参考)

来源:互联网 发布:手机抢票软件下载 编辑:程序博客网 时间:2024/05/22 13:27

explicitC# 参考)

explicit 关键字用于声明必须使用强制转换来调用的用户定义的类型转换运算符。例如,在下面的示例中,此运算符将名为 Fahrenheit 的类转换为名为 Celsius 的类:

复制代码

// Must be defined inside a class called Farenheit:

public static explicit operator Celsius(Farenheit f)

{

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

}

可以如下所示调用此转换运算符:

复制代码

Farenheit f = new Farenheit(100.0f);

Celsius c = (Celsius)f;

 备注

转换运算符将源类型转换为目标类型。源类型提供转换运算符。与隐式转换不同,必须通过强制转换的方式来调用显式转换运算符。如果转换操作可能导致异常或丢失信息,则应将其标记为 explicit。这可以防止编译器无提示地调用可能产生无法预见后果的转换操作。

省略此强制转换将导致编译时错误编译器错误 CS0266

有关更多信息,请参见使用转换运算符(C# 编程指南)

 示例

下面的示例提供 Fahrenheit Celsius 类,它们中的每一个都为另一个提供显式转换运算符。

复制代码

// cs_keyword_explicit_temp.cs

using System;

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;

    }

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

    }

}

输出

100 fahrenheit = 37.77778 celsius = 100 fahrenheit

下面的示例定义一个结构 Digit,该结构表示单个十进制数字。定义了一个运算符,用于将 byte 转换为 Digit,但因为并非所有字节都可以转换为 Digit,所以该转换是显式的。

复制代码

// cs_keyword_explicit_2.cs

using System;

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 MainClass

{

    static void Main()

    {

        try

        {

            byte b = 3;

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

        }

        catch (Exception e)

        {

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

        }

    }

}

输出

conversion occurred

 


implicitC# 参考)

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。有关更多信息,请参见使用转换运算符

输出

num = 7 dig2 = 12


operatorC# 参考)

使用 operator 关键字来重载内置运算符,或提供类或结构声明中的用户定义转换。

 示例

下面是分数的一个极其简化的类。该类重载了 + * 运算符,以执行分数加法和乘法;同时提供了将 Fraction 类型转换为 double 类型的转换运算符。

复制代码

// cs_keyword_operator.cs

using System;

class Fraction

{

    int num, den;

    public Fraction(int num, int den)

    {

        this.num = num;

        this.den = den;

    }

 

    // overload operator +

    public static Fraction operator +(Fraction a, Fraction b)

    {

        return new Fraction(a.num * b.den + b.num * a.den,

           a.den * b.den);

    }

 

    // overload operator *

    public static Fraction operator *(Fraction a, Fraction b)

    {

        return new Fraction(a.num * b.num, a.den * b.den);

    }

 

    // user-defined conversion from Fraction to double

    public static implicit operator double(Fraction f)

    {

        return (double)f.num / f.den;

    }

}

 

class Test

{

    static void Main()

    {

        Fraction a = new Fraction(1, 2);

        Fraction b = new Fraction(3, 7);

        Fraction c = new Fraction(2, 3);

        Console.WriteLine((double)(a * b + c));

    }

}

输出

0.880952380952381

 

原创粉丝点击