C# 程序员参考--用户定义地转换教学文章
本教程展示如何定义转换以及如何在类或结构之间使用转换。
示例文件
请参见“用户定义的转换”示例以下载和生成本教程中讨论的示例文件。
教程
C# 允许程序员在类或结构上声明转换,以便可以使类或结构与其他类或结构或者基本类型相互进行转换。转换的定义方法类似于运算符,并根据它们所转换到的类型命名。
在 C# 中,可以将转换声明为 implicit(需要时自动转换)或 explicit(需要调用转换)。所有转换都必须为 static,并且必须采用在其上定义转换的类型,或返回该类型。
本教程介绍两个示例。第一个示例展示如何声明和使用转换,第二个示例演示结构之间的转换。
示例 1
本示例中声明了一个 RomanNumeral
类型,并定义了与该类型之间的若干转换。
// conversion.cs using 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); } }
输出
10 Conversion not yet implemented 10
示例 2
本示例定义 RomanNumeral
和 BinaryNumeral
两个结构,并演示二者之间的转换。
// structconversion.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
[1] [2] 下一页
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); } }
输出
10 Conversion not yet implemented
代码讨论
上一页 [1] [2]
编辑推荐:
温馨提示:因考试政策、内容不断变化与调整,长理培训网站提供的以上信息仅供参考,如有异议,请考生以权威部门公布的内容为准! (责任编辑:长理培训)
点击加载更多评论>>