Skip to content

Variant delegates in variant interfaces. Why are keywords needed? #25233

@NikVladi

Description

@NikVladi

https://docs.microsoft.com/en-us/answers/questions/481118/variant-delegates-in-variant-interfaces-why-are-ke.html

I really ask you to answer my question as concretely as possible, because I'm just desperate, everyone on the forums behaves as if they understand everything, but everyone just wants to get rid of me, I just don't understand one point and ask the question.
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;
     }
 }

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions