For a long time now I have been trying to understand the usefulness of the "in" and "out" parameters in connection with generics in C # and I just can't get it into my head(I know how often this question is asked on StackOverflow). I generally understand what covariance and contravariance is, just not why "in" and "out" parameters have to be used.
The following example:
public class MainClass {
delegate TOut MyDelegate<TIn, TOut>(TIn input);
public static void Main()
{
// Func Delegate is using "in T, out TResult"
Func<Dog, Mammal> funcDelegate = TestMethod;
// not using "in" or "out" parameters
MyDelegate<Dog, Mammal> myDelegate = TestMethod;
}
static Dog TestMethod(Mammal m) { return new Dog(); }
class Mammal { }
class Dog : Mammal { } }//end of class
Why does the Func delegate make use of "in" and "out" when my own delegate without "in" and "out" can also reference a method that is covariant and contravariant?