4 ms·
It took me a while to understand it because it requires you to think differently about methods & types. Basically, a method can have inputs (parameters) and out
by tkellogg 15y ago
It took me a while to understand it because it requires you to think differently about methods & types. Basically, a method can have inputs (parameters) and outputs (return values). So if you have an interface:
public void IEnumerable<T>
{
IEnumerator<T> GetEnumerator();
}
The T type is only used in outputs in all the members. Therefore it can be declared with the `out` directive (i.e. public void IEnumerable<out T>). Covariance & contravariance doesn't work with classes, only with interfaces & delegates. Consider this delegate:
public delegate void Action<in T>(T obj);
This can be declared with covariance (the in directive) because it only uses T in it's inputs. So an Action<string> can be passed into a method that takes Action<object>.
Also, note that this doesn't work with T Func<T>(T obj) because it takes T as input & returns it (output). However, it does work with TReturn Func<in T, out TReturn>(T obj) because neither T nor TReturn is taken as both input & output.
Does this make more sense? It's a hard concept to fully understand