What is covariance and contravariance?

Experience Level: Senior
Tags: .NETASP.NET MVCASP.NET WebAPIASP.NET WebFormsC#

Answer

Covariance

Covariance enables you to pass a derived type where a base type is expected.

  • Co-variance is like variance of the same kind.
  • The base class and other derived classes are considered to be the same kind of class that adds extra functionalities to the base type. So covariance allows you to use a derived class where a base class is expected. 

Hint: can accept big if small is expected.

Links

https://blogs.msdn.microsoft.com/ericlippert/tag/covariance-and-contravariance/

Comments

No Comments Yet.
Be the first to tell us what you think.
.NET challenge
.NET challenge

Are you learning C# ? Try our test we designed to help you progress faster.

Test yourself