Variance C# with Example



Variance C# with Example

Generic interfaces and delegates can have their type parameters marked as covariant or contravariant using the out 
and in keywords respectively. These declarations are then respected for type conversions, both implicit and 
explicit, and both compile time and run time. 
For example, the existing interface IEnumerable has been redefined as being covariant: 
interface IEnumerable 
{ 
IEnumerator GetEnumerator(); 
} 
The existing interface IComparer has been redefined as being contravariant: 
public interface IComparer 
{ 
int Compare(T x, T y); 
} 

0 Comment's

Comment Form

Submit Comment