Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the name of the C# feature that allows you to implicitly consider a List<ChildClass> as a List<ParentClass>

Tags:

c#

It is relevant to a technical discussion I'm participating in.

like image 542
David Reis Avatar asked Dec 06 '22 18:12

David Reis


2 Answers

You're talking about generic covariance - but it doesn't apply to List<T>, which is invariant.

It does apply to IEnumerable<T> though:

IEnumerable<ChildClass> children = new List<ChildClass>();
IEnumerable<ParentClass> parents = children;
like image 117
Jon Skeet Avatar answered Mar 01 '23 23:03

Jon Skeet


I think you mean Covariance and Contravariance.

like image 28
Sergey Kalinichenko Avatar answered Mar 01 '23 22:03

Sergey Kalinichenko