Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda