The technical term for this is that generics are invariant in C # 3.0 and earlier. From C # 4.0 onwards, casting works.
What the invariant means is that there is no connection between the two generic types just because their common type parameters are related (i.e., they are sub- or supertypes of each other).
In your example, there is no input relationship between IEnumerable<object> and IEnumerable<string> , simply because the string is a subtype of the object. They are simply considered two completely unrelated types, such as string and int (they are still both subtypes of the object, but they all are)
There are several workarounds and exceptions for this problem that you are facing.
First, you can use each line separately for an object, if you are using .NET 3.0, you can do this using the Cast<T>() extension method. Otherwise, you can use foreach and put the result in a new variable of the static type that you want.
Secondly, arrays are an exception for the reference type, that is, passing the string [] type to the method using the object type [] should work.
Kurt schelfthout
source share