c#中结构的转化示例 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(BinaryNumeralbinary) ... { return new RomanNumeral(( int )binary); } static public explicit operator int (RomanNumeralroman) ... { return roman.value; } static public implicit operator string (RomanNumeralroman) ... { return ( " Conversionnotyetimplemented " ; } 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 (BinaryNumeralbinary) ... { return ( " Conversionnotyetimplemented " ; } static public explicit operator int (BinaryNumeralbinary) ... { return (binary.value); } private int value; } class Test ... { static public void Main() ... { RomanNumeralroman; roman = 10 ; BinaryNumeralbinary; // PerformaconversionfromaRomanNumeraltoa // BinaryNumeral: binary = (BinaryNumeral)( int )roman; // PerformsaconversionfromaBinaryNumeraltoaRomanNumeral. // Nocastisrequired: roman = binary; Console.WriteLine(( int )binary); Console.WriteLine(binary); } }