C# Covariance for EventHandler Delegate

46 Views Asked by At

C# does not like the following snippet:

public delegate void CustomEventHandler<out T>(object? sender, CustomEventArgs<T> e);

public class CustomEventArgs<T> : EventArgs
{
    public IReadOnlyList<T> Items { get; }
}

I want to make the event handler delegate covariant, but it seems that the covariance is incompatible with a concrete class, even if the concrete class uses a generic type only to instantiate an interface that is also covariant. How can the delegate be made to be covariant? I specifically don't want to drop the generic T on the CustomEventArgs class.

0

There are 0 best solutions below