Covariant can be understood as a variant, that is, converting one type to another.
Anders mentioned the variation of arrays in the array chapter of <the C # promgramiming language>.
If a reference type A can be implicitly or explicitly converted to B, A [R] can also be converted to B [R]. R can be specified arbitrarily, but a and B must be the same, this relationship is called array variation. array variations cannot be extended to value types. For example, they cannot be converted from int [] to object [] or from byte [] to int [].
Today, I found a post on Bill Wanger's blog about the variation of the function return type. C # does not support the variation of the function return type, for example, the following Code It cannot pass the mutation Class Classparent
{
Static Void Main ()
{
}
Protected Virtual Classparent getthing ()
{
Return Null;
}
}
Class Classson: classparent
{
Protected Override Classson getthing ()
{
Return Null;
}
}
An error is reported during compilation. If the method is overloaded, the return type cannot be changed, which is supported in C ++.
Bill Wanger said that it can be implemented using a model interface. My C # is 1.1 and does not support the model, but it also sticks the code below Public Interface Producer < T >
{
T getthing ();
}
Public Class D: Producer < D >
{< br> Public D getthing ()
{< br> return null ;
}
}
in this way, if the value of T is appropriately limited, the variant of the return type of the C ++ function can be implemented using the model.