<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/covariant-collections-exist-if-you-know-how--i0ffzogck" -->

---
title: Covariant Collections Exist (If You Know How) | daily.dev
description: A deep dive into why C# collections cannot be truly covariant, using arrays, generic lists, and IEnumerable to demonstrate the runtime pitfalls...
canonical: https://daily.dev/posts/covariant-collections-exist-if-you-know-how--i0ffzogck
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Covariant Collections Exist (If You Know How) | daily.dev
og:description: A deep dive into why C# collections cannot be truly covariant, using arrays, generic lists, and IEnumerable to demonstrate the runtime pitfalls...
og:url: https://daily.dev/posts/covariant-collections-exist-if-you-know-how--i0ffzogck
og:image: https://api.daily.dev/og/posts/I0ffzOGcK.png
og:image:alt: Covariant Collections Exist (If You Know How)
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Covariant Collections Exist (If You Know How)

**[Zoran Horvat](https://daily.dev/sources/zoranhorvat)** · 11 min read · 3 upvotes · 0 comments

## Summary

A deep dive into why C# collections cannot be truly covariant, using arrays, generic lists, and IEnumerable to demonstrate the runtime pitfalls (ArrayTypeMismatchException) and compile-time restrictions caused by mutable covariant types. The core technique presented builds a custom immutable IList implementation with an 'out' type parameter, implementing Add as a contravariant extension method that returns a new list instead of mutating the original, achieving safe covariance through immutability.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.youtube.com/watch?v=gWrZV1VqZKQ>

## Questions this post answers

### Why can't I make a mutable generic list in C# covariant like IList<out T>?

A type parameter declared with out can only appear in output positions, never input positions, so an Add(T item) method is rejected as invalid variance because it consumes T as input, which is contravariant. This is why no built-in mutable C# collection, including List<T>, is covariant; only read-only interfaces like IEnumerable<out T> can be.

_Developers wrestling with C# generic variance rules can dig deeper into these type system quirks on daily.dev._

### Why does casting a Giraffe array to an Animal array and inserting an Elephant throw ArrayTypeMismatchException in C#?

Arrays in C# are covariant by declared type but the CLR still tracks the array's actual runtime element type, so assigning a Giraffe[] to an Animal[] variable compiles fine, but storing an Elephant into it throws ArrayTypeMismatchException at runtime because the underlying array is really a Giraffe array. The compiler cannot catch this because it only sees the declared Animal type.

_Anyone debugging a runtime type mismatch in array covariance can compare notes on daily.dev before it bites production code._

### How can I add elements to a covariant list in C# without breaking type safety?

Define Add as an extension method rather than an interface member, since extension methods take T as an input parameter, which is naturally contravariant and does not violate the out constraint on IList<out T>. Combine this with an immutable list design so Add returns a brand-new list instance containing the added element, leaving the original list untouched and type-safe.

_Developers designing safe covariant collections in C# can trade implementation patterns on daily.dev._

## Similar posts on daily.dev

- [Kotlin Variance: Stop Confusing in & out](https://daily.dev/posts/kotlin-variance-stop-confusing-in-out-enrb55aty) · ProAndroidDev · 0 upvotes · 0 comments
- [FAQ: Why isn’t mutable a subtype of immutable, or vice versa?](https://daily.dev/posts/faq-why-isn-t-mutable-a-subtype-of-immutable-or-vice-versa--dbc7achk6) · Lobsters · 0 upvotes · 0 comments
- [Collection expressions with arguments in C\# 15](https://daily.dev/posts/collection-expressions-with-arguments-in-c-15-vqxccqqha) · Damir's Corner · 10 upvotes · 0 comments

---

Tags: [#.net](https://daily.dev/tags/.net), [#c#](https://daily.dev/tags/c#)

[View this post on daily.dev](https://daily.dev/posts/covariant-collections-exist-if-you-know-how--i0ffzogck)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Covariant Collections Exist (If You Know How)","url":"https://daily.dev/posts/covariant-collections-exist-if-you-know-how--i0ffzogck","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/covariant-collections-exist-if-you-know-how--i0ffzogck"},"datePublished":"2026-08-25T06:36:03.163Z","dateModified":"2026-09-14T07:06:58.786Z","description":"A deep dive into why C# collections cannot be truly covariant, using arrays, generic lists, and IEnumerable to demonstrate the runtime pitfalls...","image":"https://i.ytimg.com/vi/gWrZV1VqZKQ/sddefault.jpg","thumbnailUrl":"https://i.ytimg.com/vi/gWrZV1VqZKQ/sddefault.jpg","isAccessibleForFree":true,"articleSection":"Zoran Horvat","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Zoran Horvat","logo":"https://media.daily.dev/image/upload/s--TnVKcFzX--/f_auto/v1737645875/logos/zoranhorvat","url":"https://daily.dev/sources/zoranhorvat"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/covariant-collections-exist-if-you-know-how--i0ffzogck","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":3},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":".net,c#","timeRequired":"PT11M","video":{"@type":"VideoObject","name":"Covariant Collections Exist (If You Know How)","description":"A deep dive into why C# collections cannot be truly covariant, using arrays, generic lists, and IEnumerable to demonstrate the runtime pitfalls...","thumbnailUrl":"https://i.ytimg.com/vi/gWrZV1VqZKQ/sddefault.jpg","uploadDate":"2026-08-25T06:36:03.163Z","duration":"PT11M","url":"https://api.daily.dev/r/I0ffzOGcK","embedUrl":"https://www.youtube.com/embed/gWrZV1VqZKQ"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Zoran Horvat","item":"https://daily.dev/sources/zoranhorvat"},{"@type":"ListItem","position":3,"name":"Covariant Collections Exist (If You Know How)"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/covariant-collections-exist-if-you-know-how--i0ffzogck#faq","mainEntity":[{"@type":"Question","name":"Why can't I make a mutable generic list in C# covariant like IList<out T>?","acceptedAnswer":{"@type":"Answer","text":"A type parameter declared with out can only appear in output positions, never input positions, so an Add(T item) method is rejected as invalid variance because it consumes T as input, which is contravariant. This is why no built-in mutable C# collection, including List<T>, is covariant; only read-only interfaces like IEnumerable<out T> can be. Developers wrestling with C# generic variance rules can dig deeper into these type system quirks on daily.dev."}},{"@type":"Question","name":"Why does casting a Giraffe array to an Animal array and inserting an Elephant throw ArrayTypeMismatchException in C#?","acceptedAnswer":{"@type":"Answer","text":"Arrays in C# are covariant by declared type but the CLR still tracks the array's actual runtime element type, so assigning a Giraffe[] to an Animal[] variable compiles fine, but storing an Elephant into it throws ArrayTypeMismatchException at runtime because the underlying array is really a Giraffe array. The compiler cannot catch this because it only sees the declared Animal type. Anyone debugging a runtime type mismatch in array covariance can compare notes on daily.dev before it bites production code."}},{"@type":"Question","name":"How can I add elements to a covariant list in C# without breaking type safety?","acceptedAnswer":{"@type":"Answer","text":"Define Add as an extension method rather than an interface member, since extension methods take T as an input parameter, which is naturally contravariant and does not violate the out constraint on IList<out T>. Combine this with an immutable list design so Add returns a brand-new list instance containing the added element, leaving the original list untouched and type-safe. Developers designing safe covariant collections in C# can trade implementation patterns on daily.dev."}}]}
```

