The question arose about the need for keywords in variant delegates, if their events are in variant interfaces. The question only concerns the in and out keywords for delegates. See explanations below.
As soon as we make an interface with keyword in, a delegate with keyword out is required, why? After all, delagate without keyword out somehow has covariance, but only for methods, not delegate instances, which means I could put a method in event anyway, but not a delegate instance. But we cannot do this.
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//Step 1
B MyMethodReturnB()
{
return new B();
}
//Step 2
MyClass<A> MyClassForA = new MyClass<A>();
MyClassForA.MyEvent += MyMethodReturnB; //In MyClass<A>() you can put data of type B through the method MyMethodReturnB and without keyword out in the delegate MyDelegateReturn
//Step 3
MyDelegateReturn<B> MyDelegateReturnForB = new MyDelegateReturn<B>(MyMethodReturnB);
//MyClassForA.MyEvent += MyDelegateReturnForB; //But when we try to put data of type B through a delagate variable without keyword out, an error occurs, this is how variability works in delegates.
//Without the keywords in and out, one delegate cannot be assigned to another delegate
//Step 4
IMyInterface<B> IMyInterfaceForB = MyClassForA; //Through the interface IMyInterface<B> without keyword in, you cannot put MyClass<A>(), but if IMyInterface<B> with keyword in, then we can
//Step 5
//As soon as we make an interface with keyword in, a delegate with keyword out is required, why?
//After all, delagate without keyword out somehow has covariance, but only for methods, not delegate instances, which means I could put a method in event anyway, but not a delegate instance. But we cannot do this
}
}
class A
{
}
class B : A
{
}
delegate T MyDelegateReturn</*out*/ T>(); //With keyword out all works
interface IMyInterface<in T>
{
event MyDelegateReturn<T> MyEvent; //Error because delegate without keyword out
}
class MyClass<T> : IMyInterface<T>
{
public event MyDelegateReturn<T> MyEvent;
}
}