I want to implement a generic method on a generic class which would allow to cast safely, see example:
public class Foo<T> : IEnumerable<T>
{
...
public IEnumerable<R> SafeCast<R>()
where T : R
{
return this.Select(item => (R)item);
}
}
However, the compiler tells me that Foo<T>.SafeCast<R>() does not define parameter 'T'
. I understand this message that I cannot specify a constraint on T
in the method since it is not defined in the method. But how can I specify an inverse constraint?