tags:

views:

88

answers:

1

Hi,

I was wondering whether overriding conversion operators is only applicable to numeric types? If not in what instances would the implicit or explicit conversion operator be applicable to other value types or reference types. An example would be great. Thanks

+3  A: 

No - you can use any type.

For example, XAttribute has a range of conversions defined that make various conversions simple:

    XAttribute attrib = new XAttribute("Foo", "12345");
    int i = (int)attrib;
    string s = (string)attrib;

Another useful example is Jon's NonNullable<T> - this is intended to work neatly with classes while disallowing nulls. The conversion operators make it easy to use.

Marc Gravell
The mandatory "John Skeet" example :-/
Pop Catalin