Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why covariance does not work with generic method

Assume I have interface and class:

public interface ITree {}
public class Tree : ITree {}

As IEnumerable<T> is covariant, the code line below is compiled successfully:

IEnumerable<ITree> trees = new List<Tree>();

But when I put it into generic method:

public void Do<T>() where T : ITree
{
     IEnumerable<ITree> trees = new List<T>();
}

I get compiled error from compiler:

Error 1 Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?) D:\lab\Lab.General\Lab.General\Program.cs 83 40 Lab.General

Why covariance does not work in this case?

like image 486
cuongle Avatar asked Oct 05 '12 09:10

cuongle


People also ask

What is generic covariance?

Covariance and contravariance are terms that refer to the ability to use a more derived type (more specific) or a less derived type (less specific) than originally specified. Generic type parameters support covariance and contravariance to provide greater flexibility in assigning and using generic types.

What is covariance and Contravariance in generics in Java?

Covariance can be translated as "different in the same direction," or with-different, whereas contravariance means "different in the opposite direction," or against-different. Covariant and contravariant types are not the same, but there is a correlation between them.

What is the difference between covariance and Contravariance?

In C#, covariance and contravariance enable implicit reference conversion for array types, delegate types, and generic type arguments. Covariance preserves assignment compatibility and contravariance reverses it.

Can you be declared as Contravariant?

A type can be declared contravariant in a generic interface or delegate only if it defines the type of a method's parameters and not of a method's return type. In , ref , and out parameters must be invariant, meaning they are neither covariant nor contravariant.


Video Answer


1 Answers

That is because variance only works with reference types (classes, interfaces & delegates). Add a class constraint and it compiles just fine:

public static void Do<T>() where T : class, ITree
like image 93
alexn Avatar answered Oct 16 '22 13:10

alexn