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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With