Forum Xamarin.iOS

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

MonoTouch wont allow implicit covert from a subtype interface to its supertype interface

The below example compiles fine in regular Mono 2.10.9:

namespace castingtest
{      
    public class MyCloneable : ICloneable
    {
        public object Clone()
        {
            return this;
        }
    }

    public class MyClass
    {
        ICollection<MyCloneable> _values = new List<MyCloneable>();

        public IEnumerable<ICloneable> GetValues()
        {
            return _values;
        }
    }
}

However when I attempt compile it against MonoTouch 6.0.8 I get this error on the attempt to return _values:

Error CS0266: Cannot implicitly convert type 'System.Collections.Generic.ICollection<castingtest.MyCloneable>' to 'System.Collections.Generic.IEnumerable<System.ICloneable>'. An explicit conversion exists (are you missing a cast?)

If I explicitly cast to IEnumerable<ICloneable> for the return then it compiles just fine. Why do I have to be explicit for MonoTouch for something that should be implicit?

Sign In or Register to comment.