logo search
CSharp_Prog_Guide

Результат 2

conversion occurred

How to: Implement User-Defined Conversions Between Structs

This example defines two structs, RomanNumeral and BinaryNumeral, and demonstrates conversions between them.

Example

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");

}

}