Unity's version of C# implements covariance, but against any example I can find online, this code doesn't compile
string[] stringArray = new string[0];
IEnumerable<string> stringIEnumerable = new string[0];
// string[] implements IEnumerable<string>,
// and implicitly casts to IEnumerable<object> through covariance
IEnumerable<object> objects1 = stringArray;
// But puzzling enough, one cannot implicitly
// convert IEnumerable<string> to IEnumerable<object> here
IEnumerable<object> objects2 = stringIEnumerable;
Full error:
error CS0266: Cannot implicitly convert type `System.Collections.Generic.IEnumerable<string>' to `System.Collections.Generic.IEnumerable<object>'. An explicit conversion exists (are you missing a cast?)
Yes, I now cast it explicitly, but I need to know why is this happening. I know Unity's version of C# isn't the last one, but this behaviour strikes me as very odd. I'd assume that there is no intermediate version of C# that implemnets covariance without these implicit casts
Addendum: I like clean code. I want to know how to make these implicit casts work, because they feel very correct and natural in the context where I use them