- Assignment compatibility is defined in Partition I.8.7.
[Example:
Assuming Employee
:= Manager
,
IEnumerable<Manager> eManager = ...
IEnumerable<Employee> eEmployee = eManager; // Covariance
IComparer<object> objComp = ...
IComparer<string> strComp = objComp; // Contravariance
EventHandler<Employee> employeeHandler = ...
EventHandler<Manager> managerHandler = employeeHandler; // Contravariance
end example]
[Example:
Given the following:
interface IConverter<-T,+U> {
U Convert(T x);
}
IConverter<string, object> := IConverter<object, string>
Given the following:
delegate U Function<-T,+U>(T arg);
Function<string, object> := Function<object, string>.
end example]
[Example:
IComparer<object> objComp = ...
// Contravariance and interface inheritance
IKeyComparer<string> strKeyComp = objComp;
IEnumerable<string[]> strArrEnum = …
// Covariance on IEnumerable and covariance on arrays
IEnumerable<object[]> objArrEnum = strArrEnum;
IEnumerable<string>[] strEnumArr = ...
// Covariance on IEnumerable and covariance on arrays
IEnumerable<object>[] objEnumArr = strEnumArr;
IComparer<object[]> objArrComp = ...
// Contravariance on IComparer and covariance on arrays
IComparer<string[]> strArrComp = objArrComp;
IComparer<object>[] objCompArr = ...
// Contravariance on IComparer and covariance on arrays
IComparer<string>[] strCompArr = objCompArr;
end example]