Skip to content

Instantly share code, notes, and snippets.

@jjokela
Created August 13, 2014 11:17
Show Gist options
  • Save jjokela/975c93114da60e21cbe4 to your computer and use it in GitHub Desktop.
Save jjokela/975c93114da60e21cbe4 to your computer and use it in GitHub Desktop.
Covariance and Contravariance
// Airplane = base class
// F16 = derived class
// covariance: can use MORE spesific type than originally specified
IEnumerable<F16> f16s = new List<F16>();
IEnumerable<Airplane> covariance = f16s;
// contravariance: can use LESS specific type than originally specified
Action<Airplane> airplanes = (target) => { Console.WriteLine(target.GetType().Name); };
Action<F16> contravariance = airplanes;
contravariance(new F16());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment