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.
Once we create an interface with the in keyword, we need a delegate with the out keyword, why? After all, delagate without the out keyword somehow has covariance, but only for methods, not delegate instances, which means I could have put the method on the event anyway, but not the 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 using the MyMethodReturnB method and without the out keyword in the MyDelegateReturn delegate
// Step 3
MyDelegateReturn <B> MyDelegateReturnForB = new MyDelegateReturn <B> (MyMethodReturnB);
//MyClassForA.MyEvent + = MyDelegateReturnForB; // But when we try to pass data of type B through a delegate variable without the out keyword, an error occurs, this is how variability works in delegates.
// Without the in and out keywords, one delegate cannot be assigned to another delegate
// Step 4
IMyInterface <B> IMyInterfaceForB = MyClassForA; // Through the IMyInterface <B> interface without the in keyword, you cannot put MyClass <A> (), but if IMyInterface <B> with the in keyword, then we can
// Step 5
// Once we create an interface with the in keyword, we need a delegate with the out keyword, why?
// In the end, delagate without the out keyword somehow has covariance, but only for methods, not delegate instances, which means I could have put the method on the event anyway, but not the delegate instance. But we can't do this
}
}
class A
{
}
class B: A
{
}
delegate T MyDelegateReturn </ * out * / T> (); // Everything works with the out keyword
interface IMyInterface <in T>
{
event MyDelegateReturn <T> MyEvent; // Error due to delegate without out keyword
}
class MyClass <T>: IMyInterface <T>
{
public event MyDelegateReturn <T> MyEvent;
}
}