C# enables programmers to declare conversions on classes or structs so that classes or structs can be converted to and/or from other classes or structs, or basic types. Conversions are defined like operators and are named for the type to which they convert. Either the type argument to be converted, or the type of the result of the conversion, but not both, must be the containing type.
class SampleClass
{
public static explicit operator SampleClass(int i)
{
SampleClass temp = new SampleClass();
// code to convert from int to SampleClass...
return temp;
}
}
Conversion Operators Overview
Conversion operators have the following properties:
Conversions declared as implicit occur automatically when required.
Conversions declared as explicit require a cast to be called.
All conversions must be static.
Using Conversion Operators
Conversion operators can be explicit or implicit. Implicit conversion operators are easier to use, but explicit operators are useful when you want users of the operator to be aware that a conversion is taking place. This topic demonstrates both types.
This is an example of an explicit conversion operator. This operator converts from the type Byte to a value type called Digit. Because not all bytes can be converted to a digit, the conversion is explicit, meaning that a cast must be used, as shown in the Main method.
struct Digit
{
byte value;
public Digit(byte value) //constructor
{
if (value > 9)
{
throw new System.ArgumentException();
}
this.value = value;
}
public static explicit operator Digit(byte b) // explicit byte to digit conversion operator
{
Digit d = new Digit(b); // explicit conversion
System.Console.WriteLine("conversion occurred");
return d;
}
}
class TestExplicitConversion
{
static void Main()
{
try
{
byte b = 3;
Digit d = (Digit)b; // explicit conversion
}
catch (System.Exception e)
{
System.Console.WriteLine("{0} Exception caught.", e);
}
}
}
This example demonstrates an implicit conversion operator by defining a conversion operator that undoes what the previous example did: it converts from a value class called Digit to the integral Byte type. Because any digit can be converted to a Byte, there's no need to force users to be explicit about the conversion.
struct Digit
{
byte value;
public Digit(byte value) //constructor
{
if (value > 9)
{
throw new System.ArgumentException();
}
this.value = value;
}
public static implicit operator byte(Digit d) // implicit digit to byte conversion operator
{
System.Console.WriteLine("conversion occurred");
return d.value; // implicit conversion
}
}
class TestImplicitConversion
{
static void Main()
{
Digit d = new Digit(3);
byte b = d; // implicit conversion -- no cast needed
}
}
This example defines two structs, RomanNumeral and BinaryNumeral, and demonstrates conversions between them.
struct RomanNumeral
{
private int value;
public RomanNumeral(int value) //constructor
{
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");
}
}
struct BinaryNumeral
{
private int value;
public BinaryNumeral(int value) //constructor
{
this.value = value;
}
static public implicit operator BinaryNumeral(int value)
{
return new BinaryNumeral(value);
}
static public explicit operator int(BinaryNumeral binary)
{
return (binary.value);
}
static public implicit operator string(BinaryNumeral binary)
{
return ("Conversion not yet implemented");
}
}
class TestConversions
{
static void Main()
{
RomanNumeral roman;
BinaryNumeral binary;
roman = 10;
// Perform a conversion from a RomanNumeral to a BinaryNumeral:
binary = (BinaryNumeral)(int)roman;
// Perform a conversion from a BinaryNumeral to a RomanNumeral:
// No cast is required:
roman = binary;
System.Console.WriteLine((int)binary);
System.Console.WriteLine(binary);
}
}
In the previous example, the statement:
binary = (BinaryNumeral)(int)roman;
performs a conversion from a RomanNumeral to a BinaryNumeral. Because there is no direct conversion from RomanNumeral to BinaryNumeral, a cast is used to convert from a RomanNumeral to an int, and another cast to convert from an int to a BinaryNumeral.
roman = binary;
performs a conversion from a BinaryNumeral to a RomanNumeral. Because RomanNumeral defines an implicit conversion from BinaryNumeral, no cast is required.
No comments:
Post a Comment