本教程展示如何定义转换以及如何在类或结构之间使用转换。
请参见“用户定义的转换”示例以下载和生成本教程中讨论的示例文件。
C# 允许程序员在类或结构上声明转换,以便可以使类或结构与其他类或结构或者基本类型相互进行转换。转换的定义方法类似于运算符,并根据它们所转换到的类型命名。
在 C# 中,可以将转换声明为 implicit(需要时自动转换)或 explicit(需要调用转换)。所有转换都必须为 static,并且必须采用在其上定义转换的类型,或返回该类型。
本教程介绍两个示例。第一个示例展示如何声明和使用转换,第二个示例演示结构之间的转换。
本示例中声明了一个 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
本示例定义 RomanNumeral
和 BinaryNumeral
两个结构,并演示二者之间的转换。
// 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;
执行从 RomanNumeral
到 BinaryNumeral
的转换。由于没有从 RomanNumeral
到 BinaryNumeral
的直接转换,所以使用一个转换将 RomanNumeral
转换为 int,并使用另一个转换将 int 转换为 BinaryNumeral
。
roman = binary;
执行从 BinaryNumeral
到 RomanNumeral
的转换。由于 RomanNumeral
定义了从 BinaryNumeral
的隐式转换,所以不需要转换。