Skip to main content

What is covariance and contravariance?

Junior C#
Quick Answer These control how generic types relate when type parameters have an inheritance relationship. Covariance (out T) lets you use IEnumerable where IEnumerable is expected รขโ‚ฌโ€ safe because you only read. Contravariance (in T) allows Action where Action is expected รขโ‚ฌโ€ safe because you only write.

Answer

Covariance: Allows using a more derived type than originally specified.

Contravariance: Allows using a more base type than originally specified.

S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice