3

The following code doesn't compile, and I fail to understand why

namespace ImplicitConversion
{
    struct Wrapper<T>
    {
        public static implicit operator Wrapper<T> (T input)
        {
            return new Wrapper<T> ();
        }
    }

    interface IFoo
    {
    }

    class Foo:IFoo
    {
    }

    class MainClass
    {
        public static void Main (string[] args)
        {       
        }

        static Wrapper<IFoo> Test ()
        {
            IFoo foo = new Foo ();
            return foo; // Cannot implicitly convert type 'ImplicitConversion.IFoo' to 'ImplicitConversion.Wrapper<ImplicitConversion.IFoo>' (CS0029) (ImplicitConversion)
        }
    }
}

Why can't I use the implicit conversion?

miniBill
  • 1,743
  • 17
  • 41
  • 1
    this doesn't really have anything to do with it being an async method btw. this conversion fails whether it's a `Task>` or just a `Wrapper` – DLeh Jan 15 '15 at 14:16
  • Also, it seems that serdar is right, you cannot implicitly convert from interfaces: http://stackoverflow.com/a/143567/526704 – DLeh Jan 15 '15 at 14:18
  • @DLeh you're right. I've edited the question – miniBill Jan 15 '15 at 14:19
  • Was also going to wield the dupehammer, but to this: http://stackoverflow.com/questions/1208796/c-sharp-compiler-bug-why-doesnt-this-implicit-user-defined-conversion-compile – spender Jan 15 '15 at 14:21

1 Answers1

2

As DLeh mentioned it is not about async.

AFAIK interfaces can not be converted(either implicit or explicit).

See this answer for details: https://stackoverflow.com/a/143567/526704

Community
  • 1
  • 1
serdar
  • 1,564
  • 1
  • 20
  • 30