Covariance And Contravariance In C#

2020. 1. 24. 03:00카테고리 없음

Covariance And Contravariance In C# Covariance
  1. C# Covariance And Contravariance Explained
Covariance

C# Covariance And Contravariance Explained

Mar 10, 2009 - In C# roughly we can say that covariance means we can substitute derived type in place of base type. Contravariance means we can substitute.

C’s classical OOP system supports “covariant return types,” but it does not support “contravariant parameter types.”The problem in the authors 'Animal doctor' example is that the universe of all possible animal types is practically unbounded, or 'open'.What you want here are 'open multimethods': something Bjarne Stroustrup has wanted in C for a long time0 but have been implemented in library form1.Of course, the thing to remember is that, as soon as you go to an open multiple dispatch arrangement, you will lose much of the static safety guarantees that the type-system affords us. There will always be runtime/dynamic type checking, and a runtime failure case.If you don't want an open system, then some combination of templates for static polymorphism and strategic type-erasure will get the job done. Use boost::variant and multivisitation 2, and the compiler will instantiate all O(n^2) methods for you.0 (2007)12.

Covariance And Contravariance In C#