Skip to main content

What is covariance and contravariance in C# generics?

Expert C#
Quick Answer Covariance (out T on interfaces like IEnumerable) lets you use a more derived type where a base is expected รขโ‚ฌโ€ safe for read-only scenarios. Contravariance (in T on Action) lets you use a more base type รขโ‚ฌโ€ safe for write-only scenarios. Applies to interfaces and delegates, not classes.

Answer

Covariance allows using derived types where base types are expected. Contravariance allows the opposite.

They enable flexible delegate and interface usage.

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