C# 程序员参考--用户定义的转换教程(转)

本文介绍了C#中用户定义转换的基本概念,并通过实例展示了如何在不同结构间定义和使用显式及隐式转换。

本教程展示如何定义转换以及如何在类或结构之间使用转换。

示例文件

请参见“用户定义的转换”示例以下载和生成本教程中讨论的示例文件。

教程

C# 允许程序员在类或结构上声明转换,以便可以使类或结构与其他类或结构或者基本类型相互进行转换。转换的定义方法类似于运算符,并根据它们所转换到的类型命名。

在 C# 中,可以将转换声明为 implicit(需要时自动转换)或 explicit(需要调用转换)。所有转换都必须为 static,并且必须采用在其上定义转换的类型,或返回该类型。

本教程介绍两个示例。第一个示例展示如何声明和使用转换,第二个示例演示结构之间的转换。

示例 1

本示例中声明了一个 RomanNumeral 类型,并定义了与该类型之间的若干转换。

// conversion.csusing System;struct RomanNumeral{    public RomanNumeral(int value)     {        this.value = value;     }    // Declare a conversion from an int to a RomanNumeral. Note the    // the use of the operator keyword. This is a conversion     // operator named RomanNumeral:    static public implicit operator RomanNumeral(int value)     {       // Note that because RomanNumeral is declared as a struct,        // calling new on the struct merely calls the constructor        // rather than allocating an object on the heap:       return new RomanNumeral(value);    }    // Declare an explicit conversion from a RomanNumeral to an int:    static public explicit operator int(RomanNumeral roman)    {       return roman.value;    }    // Declare an implicit conversion from a RomanNumeral to     // a 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;// Call the explicit conversion from numeral to int. Because it is// an explicit conversion, a cast must be used:        Console.WriteLine((int)numeral);// Call the implicit conversion to string. Because there is no// cast, the implicit conversion to string is the only// conversion that is considered:        Console.WriteLine(numeral); // Call the explicit conversion from numeral to int and // then the explicit conversion from int to short:        short s = (short)numeral;        Console.WriteLine(s);    }}
输出
10Conversion not yet implemented10
示例 2

本示例定义 RomanNumeralBinaryNumeral 两个结构,并演示二者之间的转换。

// structconversion.csusing 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;        // Perform a conversion from a RomanNumeral to a        // BinaryNumeral:        binary = (BinaryNumeral)(int)roman;        // Performs a conversion from a BinaryNumeral to a RomanNumeral.        // No cast is required:        roman = binary;        Console.WriteLine((int)binary);        Console.WriteLine(binary);    }}
输出
10Conversion not yet implemented
代码讨论
  • 在上个示例中,语句
    binary = (BinaryNumeral)(int)roman;

    执行从 RomanNumeralBinaryNumeral 的转换。由于没有从 RomanNumeralBinaryNumeral 的直接转换,所以使用一个转换将 RomanNumeral 转换为 int,并使用另一个转换将 int 转换为 BinaryNumeral

  • 另外,语句
    roman = binary;

    执行从 BinaryNumeral RomanNumeral 的转换。由于 RomanNumeral 定义了从 BinaryNumeral 的隐式转换,所以不需要转换。

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8781179/viewspace-924167/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8781179/viewspace-924167/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值