Tuesday, 13 August 2013

Implemented interface with co/contravariance generates compile time error

Implemented interface with co/contravariance generates compile time error

I have implemented an interface with co/contravariant type constraints and
the compiler is telling me that 'Student' must be a non-abstract type with
a public parameterless constructor in order to use it as parameter 'T' in
the generic type or method 'UserQuery.IMail<T,U>'
From what I can see, I'm satisfying those requirements. What am I doing
wrong?
class Person { public Person(){} }
class Student : Person { public Student(){} }
class MatureStudent : Student {public MatureStudent(){}}
interface IMail<in T, out U> where T : new() where U : new() {
void Receive(T t);
U Return();
}
class Mail<Student,MatureStudent> : IMail<Student,MatureStudent> {
public void Receive(Student s) {}
public MatureStudent Return() { return new MatureStudent(); }
}

No comments:

Post a Comment