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

371
Visualizações
Why is this cast redundant?

I have a method with the following overloads:

string Call(string function, Dictionary<string, object> parameters, object body)
string Call(string function, Dictionary<string, object> parameters, JObject body)

Now I added another overload:

string Call(string function)
{
    return Call(function, null, (JObject) null);
}

I added a cast to JObject so the compiler knows which overload it should use. But Visual Studio tells me that the cast is redundant. But why isn't my call ambiguous without the cast?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

But why isn't my call ambiguous without the cast?

Because the overload with the JObject parameter is "better" than the overload with the object parameter... because the conversion from null to JObject is "better" than the conversion from null to object.

JObject is more specific than object, because there's an implicit conversion from JObject to object, but not vice versa.

If the final parameter for the first method were string instead (for example) then neither overload would be better than the other, and the call would be ambiguous without the cast.

See section 7.5.3 of the C# 5 specification for all the intricate details. In particular, section 7.5.3.5 ("better conversion target") is relevant here.

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