From Java to C#: A Developers Guide

23.2 Syntax of user -defined conversion method declarations

As with operator overloading, you use the operator keyword to declare the conversion method. The full syntax for the declaration of the conversion method is:

public static <explicitexplicit> operator conv_type_out (conv_type_in operand)

Additional notes

  • Besides classes, you can also write conversion methods for structs (see Chapter 26).

  • The conversion method must be static.

  • The conversion method must either return the type for which the conversion is defined, or take that type in as one of the method parameters.

Категории