Practice mode C#

In C#, why can an IEnumerable be assigned to a variable of type IEnumerable?

Correct answer A. Because IEnumerable<out T> is covariant in T

Explanation

IEnumerable declares its type parameter as out T, making it covariant: a sequence of a more derived type can be used as a sequence of a less derived one, since items are only read. List is not covariant, so List cannot be assigned to List. Variance applies only to interfaces and delegates, and only with reference types.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.