List<T>
is not covariant, your method will work with just an IEnumerable<>
:
public string CombineAllFields(IEnumerable<GenericNameValue> mylist)
{
.... do stuff with each item
}
The full definitions from the library:
public class List<T> : IList<T> { }
public interface IList<T> : IEnumerable<T> { }
public interface IEnumerable<out T> { }
Note that your down-cast can only work with an interface that uses that out
modifier.
Consider that using either a List<T>
or an IList<T>
parameter would allow your method to change the List. Deletions wouldn't matter but we have to prevent adding a GenericNameValue
to a List<SpecificNameValue>
. IEnumerable<>
won't let you add to a collection so covariance is safe.