What is covariance and contravariance?

Experience Level: Senior
Tags: .NET ASP.NET MVC ASP.NET WebAPI ASP.NET WebForms C#

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/

Related C# job interview questions
.NET challenge
.NET challenge

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

Test yourself