Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
Contravariance invalid when using interface's delegate as a parameter type

Consider the contravariant interface definition with a delegate:

public interface IInterface<in TInput>
{
    delegate int Foo(int x);
    
    void Bar(TInput input);
    
    void Baz(TInput input, Foo foo);
}

The definition of Baz fails with an error:

CS1961
Invalid variance: The type parameter 'TInput' must be covariantly valid on 'IInterface<TInput>.Baz(TInput, IInterface<TInput>.Foo)'. 'TInput' is contravariant.

My question is why? On first glance this should be valid, as the Foo delegate has nothing to do with TInput. I don't know if it's the compiler being overly conservative or if I'm missing something.

Note that normally you wouldn't declare a delegate inside an interface, in particular this doesn't compile on versions older than C# 8, since a delegate in an interface needs default interface implementations.

Is there a way to break the type system if this definition was allowed, or is the compiler conservative?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I am not sure if this is co- versus contravariance problem.

  1. The Foo delegate is not a member of the interface. It is a nested type declaration.
  2. IInterface<A>.Foo and IInterface<B>.Foo are two different types.
  3. This makes the foo parameter of two different IInterface<T>.Baz methods (with T = A and B) incompatible.
  4. Therefore you cannot substitute a IInterface<A> for a IInterface<B> or vice-versa (no matter what the inheritance relationship between A and B is.
  5. Conclusion: IInterface<T> cannot be variant (neither co- nor contra-).

Resolution:

  • Move the delegate to the top level (in the body of a namespace). It is a type declaration, so, it does not need to be embedded.
  • Or embed it in a type with no type parameter. E.g., you could create a non-generic IInterface for this (and keep your generic one).

But @EricLippert certainly knows better.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda