Example of Structure Conversion in C #
Structconversion. CS
Using System;
Struct Romannumeral
... {
Public Romannumeral ( Int Value)
... {
This. Value=Value;
}
Static Public Implicit Operator Romannumeral ( Int Value)
... {
Return NewRomannumeral (value );
}
Static Public Implicit Operator Romannumeral (binarynumeral binary)
... {
Return NewRomannumeral ((Int) Binary );
}
Static Public Explicit Operator Int (Romannumeral Roman)
... {
ReturnRoman. 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 NewBinarynumeral (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
// 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 );
}
}