Two possibilities.
One, redefine void SomeMethod(List<X> list)
as void SomeMethod(IEnumerable<X> list)
.
Because the IEnumerable<T>
is covariant, you can pass an IEnumerable<A>
as to a method with an IEnumerable<X>
parameter, and it will work.
Two, redefine void SomeMethod(List<X> list)
as void SomeMethod<T>(List<T> list) where T : X
.
Here you have a generic SomeMethod
that will work with lists of any type derived from X
, including X
itself.
(Even better if you can is to define the method as void SomeMethod<T>(List<T> list)
[without the where
] or better still, as SomeMethod<T>(IEnumerable<T> list)
as then you've a method applicable to even more cases than those you are currently thinking about, but I'm going to assume you need to make use of some features of X
and those therefore aren't viable).
Note also that as people have said the ref
is unusual here (I just took it out in the above), and it also causes further problems. To use ref
you need an assignable variable (a local variable, a parameter, a settable property or an array access) of the appropriate type, as ref
needs to be able to assign as well as access. Hence you can never cast when passing to a ref
variable as SomeMethod(ref (List<X>)listA)
attempts to do, even if listA
could be cast to List<X>
(which it can't anyway.