0

Why does implicit operator have problems when I try to convert the return type to an object or a one custom type that I have defined myself?

I have no problem when I am trying to convert and return a simple type like an int, like this

public static implicit operator int(MyClass a)
        {
            return 123;
        }

However, when I try to convert the type into an object or a custom type, I will get an error. For example, I will get a compile-time error saying "User-defined conversion to System.Object" if I have something like this:

public static implicit operator object(MyClass a)
{
   return new {};
}

I will get the same kind of error if I have this:

public static implicit operator CustomTestObject(MyClass a)
{
   return new CustomTestObject();
}

How can I convert it to my own custom defined type for return when using an implicit operator?

5
  • I can't see why the CustomTestObject conversion wouldn't work. Commented Dec 30, 2012 at 21:20
  • @mikez I too would like to see the exact compiler message for the last conversion. Also I would like to know what class (or struct) it is inside, the inheritance chain of that class, and the inheritance chains of MyClass and CustomTestObject. In my answer I kind of assumed that one was a base class of the other. Commented Dec 30, 2012 at 21:33
  • @JeppeStigNielsen yes that would explain it. Commented Dec 30, 2012 at 21:38
  • MyClass is an ordinary class that inherits from an Object base class. I can understand from your answer how implicitly converting it to object in this case wouldn't have worked. I just realised that in my actual code, the example CustomTestObject is an interface. I believe that since an interface doesn't have any concrete implementation, the conversion wouldn't make sense. Thanks a lot for the answer. Commented Dec 30, 2012 at 21:46
  • Userdefined conversion (implicit og explicit operator) is not allowed to or from interfaces. The error message you got should mention interfaces (your question didn't). Some derived class of your class could start implementing the interface. Then it would be confusing for that derived class to be implicitly convertible to the interface (by a simple reference conversion) because it implements it, while at the same time having a user-defined conversion to the same interface. I think that's one of the reasons why they disallowed conversions to/from interfaces. Commented Jan 8, 2013 at 14:30

1 Answer 1

5

You cannot declare an implicit conversion to one of your own base classes. System.Object is always your (direct or indirect) base class. Through inheritance there's already an implicit conversion to the base class. So how could there be two implicit conversions? The C# spec has a good reson to disallow this!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.