Skip to content

Latest commit

 

History

History
67 lines (46 loc) · 1.58 KB

ii.9.6-assignment-compatibility-of-instantiated-types.md

File metadata and controls

67 lines (46 loc) · 1.58 KB

II.9.6 Assignment compatibility of instantiated types

[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]