The fact that int
is a value type makes the difference here. According to the C # specification, there must be reference or identifier reference between typical types in order to make the types optionally convertible:
A type T<A1, …, An>
is variance-convertible for a type T<B1, …, Bn>
if T
it is either an interface or a delegate type declared with a type variant T<X1, …, Xn>
and, for each type variant parameter, Xi
one of the following assertions:
-
Xi
is covariant and there is an implicit reference or identity reversal from Ai
toBi
-
Xi
is contravariant and there is an implicit reference or identity reversal from Bi
toAi
-
Xi
is invariant and there is an identical transformation from Ai
toBi
IEnumerable<T>
is covariant, so the marked line i is important. Ai
in your case int
it Bi
is int
. There is no reference conversion from int
to object
, because it is int
not a reference type. There is also no identity conversion between the two because they are not the same.
I don't know the whole context of your question, but you can use non-generic IEnumerable
instead IEnumerable<object>
. They are almost the same. However, you may run into the XY problem here, so it is difficult for you to help without knowing more about your case.
source
to share