A new feature of. NET 4.O is the covariant and inverter. The covariant and inverter allow implicit conversion of array type, delegate type, and generic type parameters. The Coordination variable retains the allocation compatibility, which is opposite to the inverter.
I. covariant
1. Let's look at an example before introducing the concept.
/// <Summary>
/// Compilation error before. NET 4.0
/// </Summary>
Class Program
{
Public abstract class Person {public string Name {get; set ;}}
Public class Student: Person {}
Public class Teacher: Person {}
Static void Main ()
{
Student student = new Student ();
Person person = student; // here, the base class points to the reference of the subclass, compiled through
IEnumerable <Student> students = null;
IEnumerable <Person> persons = students; // an error is reported during compilation.
}
}
The assignment operation in. NET 4.0 is effective because the definition of IEnumerable <T> has changed: view the original article