I've encountered an interesting case when playing with implicit casts and IEnumerable - please look at the last line of attached code - it won't compile.
public class Outer<T>
{
private T field;
public Outer(T it)
{
field = it;
}
public static implicit operator Outer<T> (T source)
{
return new Outer<T>(source);
}
}
void Main()
{
Outer<string> intsample = "aa";
Outer<IList<object>> listsample = new List<object>();
//The line below fails with:
//CS0266 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<object>'
//to 'UserQuery.Outer<System.Collections.Generic.IEnumerable<object>>'.
//An explicit conversion exists (are you missing a cast?)
Outer<IEnumerable<object>> enumerablesample = Enumerable.Empty<object>();
}
I have a very strong gut feeling, that it is elated to IEnumerable
being covariant, but could someone explain that more formal?