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/
Related C# job interview questions
What is a namespace in C# and what is it good for?
C# JuniorWhat is abstract class and what is it good for?
C# MediorHave you ever consumed some 3rd party API? What was it? Have you ever designed your own API? Could you describe it a bit?
.NETArchitectureASP.NET WebAPIC#Web services MediorWhat is serialization/deserialization? What types of serialization do you know?
.NETC# MediorWhat is a MultiCastDelegate? What is it good for? Give some real use case
.NETC# Senior