#1,157 – Contravariance and Generic Delegate Types
August 11, 2014 2 Comments
As with generic interfaces, generic delegate types are contravariant if you mark input parameters as in.
In the example below, we’re unable to assign an instance of ReturnDelegate<Dog> to ReturnDelegate<Terrier>. The delegate type is not contravariant.
public delegate void ReportOnDelegate<T>(T param); public static void ReportOnDog(Dog d) { Console.WriteLine(d.Name); } static void Main(string[] args) { ReportOnDelegate<Dog> del = ReportOnDog; // Error: Cannot implicitly convert ReportOnDelegate<Dog> // to ReportOnDelegate<Terrier> ReportOnDelegate<Terrier> del2 = del; }
We can get the delegate type to behave contravariantly by marking its type parameter with the in keyword.
public delegate void ReportOnDelegate<in T>(T param);
We can now assign an instance of ReportOnDelegate<Dog> to a variable of type ReportOnDelegate<Terrier>.