本教程展示如何定義轉換以及如何在類或結構之間使用轉換。
請參見“用戶定義的轉換”示例以下載和生成本教程中討論的示例文件。
C# 允許程序員在類或結構上聲明轉換,以便可以使類或結構與其他類或結構或者基本類型相互進行轉換。轉換的定義方法類似於運算符,並根據它們所轉換到的類型命名。
在 C# 中,可以將轉換聲明為 implicit(需要時自動轉換)或 explicit(需要調用轉換)。所有轉換都必須為 static,並且必須采用在其上定義轉換的類型,或返回該類型。
本教程介紹兩個示例。第一個示例展示如何聲明和使用轉換,第二個示例演示結構之間的轉換。
本示例中聲明了一個 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
本示例定義 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
binary = (BinaryNumeral)(int)roman;
執行從 RomanNumeral
到 BinaryNumeral
的轉換。由於沒有從 RomanNumeral
到 BinaryNumeral
的直接轉換,所以使用一個轉換將 RomanNumeral
轉換為 int,並使用另一個轉換將 int 轉換為 BinaryNumeral
。
roman = binary;
執行從 BinaryNumeral
到 RomanNumeral
的轉換。由於 RomanNumeral
定義了從 BinaryNumeral
的隱式轉換,所以不需要轉換。
上一頁 [1] [2]