Procedimiento Implementar conversiones definidas por el usuario entre structs (Guía de programación de C#)How to: Implement User-Defined Conversions Between Structs (C# Programming Guide)

Este ejemplo define dos structs, RomanNumeral y BinaryNumeral, y muestra conversiones entre ellos.This example defines two structs, RomanNumeral and BinaryNumeral, and demonstrates conversions between them.

EjemploExample

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 to string is not 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 to string is not 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);

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
/* Output:
    10
    Conversion to string is not implemented
*/

Programación sólidaRobust Programming

  • En el ejemplo anterior, la instrucción:In the previous example, the statement:

    binary = (BinaryNumeral)(int)roman;
    

    realiza una conversión de RomanNumeral a BinaryNumeral.performs a conversion from a RomanNumeral to a BinaryNumeral. Como no existe una conversión directa de RomanNumeral a BinaryNumeral, se usa una conversión explícita de un RomanNumeral a un int, y otra conversión explícita para convertir de un int a un 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.

  • Además la instrucciónAlso the statement

    roman = binary;
    

    realiza una conversión de BinaryNumeral a RomanNumeral.performs a conversion from a BinaryNumeral to a RomanNumeral. Como RomanNumeral define una conversión implícita desde BinaryNumeral, no se requiere ninguna conversión explícita.Because RomanNumeral defines an implicit conversion from BinaryNumeral, no cast is required.

Vea tambiénSee also