• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C#如何定义与类或结构之间的转换

OC/C/C++ 水墨上仙 2076次浏览

本示例演示如何定义与类或结构之间的转换,以及如何使用此类转换

// conversion.cs
using System;
struct RomanNumeral
{
    public RomanNumeral(int value) 
    { 
       this.value = value; 
    }
    // 声明从 int 到 RomanNumeral 的转换。请注意
    // operator 关键字的使用。这是名为 
    // RomanNumeral 的转换运算符:
    static public implicit operator RomanNumeral(int value) 
    {
       // 请注意,由于 RomanNumeral 声明为结构,
       // 因此对该结构调用 new 只是调用构造函数
       // 而不是在堆上分配对象:
       return new RomanNumeral(value);
    }
    // 声明从 RomanNumeral 到 int 的显式转换:
    static public explicit operator int(RomanNumeral roman)
    {
       return roman.value;
    }
    // 声明从 RomanNumeral 到
    // string 的隐式转换:
    static public implicit operator string(RomanNumeral roman)
    {
       return("Conversion not yet implemented");
    }
    private int value;
}
class Test
{
    static public void Main()
    {
        RomanNumeral numeral;
        numeral = 10;
// 调用从 numeral 到 int 的显式转换。由于是显式转换,
// 因此必须使用强制转换:
        Console.WriteLine((int)numeral);
// 调用到 string 的隐式转换。由于没有
// 强制转换,到 string 的隐式转换是可以考虑的
// 唯一转换:
        Console.WriteLine(numeral);
 
// 调用从 numeral 到 int 的显式转换,
// 然后调用从 int 到 short 的显式转换:
        short s = (short)numeral;
        Console.WriteLine(s);
    }
}

//&nbspstructconversion.cs

using System;
struct RomanNumeral
{
    public RomanNumeral(int value) 
    {
        this.value = value; 
    }
    static public implicit operator RomanNumeral(int value)
    {
        return new RomanNumeral(value);
    }
    static public implicit operator RomanNumeral(BinaryNumeral binary)
    {
        return new RomanNumeral((int)binary);
    }
    static public explicit operator int(RomanNumeral roman)
    {
         return roman.value;
    }
    static public implicit operator string(RomanNumeral roman) 
    {
        return("Conversion not yet implemented");
    }
    private int value;
}
struct BinaryNumeral
{
    public BinaryNumeral(int value) 
    {
        this.value = value;
    }
    static public implicit operator BinaryNumeral(int value)
    {
        return new BinaryNumeral(value);
    }
    static public implicit operator string(BinaryNumeral binary)
    {
        return("Conversion not yet implemented");
    }
    static public explicit operator int(BinaryNumeral binary)
    {
        return(binary.value);
    }
    private int value;
}
class Test
{
    static public void Main()
    {
        RomanNumeral roman;
        roman = 10;
        BinaryNumeral binary;
        // 执行从 RomanNumeral 到
        // BinaryNumeral 的转换:
        binary = (BinaryNumeral)(int)roman;
        // 执行从 BinaryNumeral 到 RomanNumeral 的转换。
        // 不需要任何强制转换:
        roman = binary;
        Console.WriteLine((int)binary);
        Console.WriteLine(binary);
    }
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C#如何定义与类或结构之间的转换
喜欢 (0)
加载中……