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?

4
  • 1
    this doesn't really have anything to do with it being an async method btw. this conversion fails whether it's a Task<Wrapper<IFoo>> or just a Wrapper<IFoo> Commented Jan 15, 2015 at 14:16
  • Also, it seems that serdar is right, you cannot implicitly convert from interfaces: stackoverflow.com/a/143567/526704 Commented Jan 15, 2015 at 14:18
  • @DLeh you're right. I've edited the question Commented Jan 15, 2015 at 14:19
  • Was also going to wield the dupehammer, but to this: stackoverflow.com/questions/1208796/… Commented Jan 15, 2015 at 14:21

1 Answer 1

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

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

2 Comments

Yup, 6.4.4 in the C# spec. (Also if you remove the generics, you get an explicit 'User-defined conversion from interface' error.)
I've modified the question

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.