I have the following class and extension class (for this example):
public class Person<T>
{
public T Value { get; set; }
}
public static class PersonExt
{
public static void Process<TResult>(this Person<IEnumerable<TResult>> p)
{
// Do something with .Any().
Console.WriteLine(p.Value.Any());
}
}
I was expecting I could write something like the following and it would work, but it doesn't:
var x = new Person<List<String>>();
x.Process();
Since List is lower in the inheritance tree than IEnumerable, shouldn't this be valid? It works if I new up a Person<IEnumerable<String>>
of course because that's the direct type.
I'm trying to use an extension method that can be applied to all Person<T>
's as long as T implements IEnumerable<Something>
because I need to use the .Any()
method.
EDIT: Maybe my understanding of covariance is off? I know IEnumerable<String>
should convert to IEnumerable<Object>
, but couldn't IList<String>
convert to IEnumerable<String>
?
EDIT2: Forgot to mention that I am using .net 4.0.