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

266
Visualizações
Why is Use of an unassigned local variable allowed here?

Normally the compiler forbids any use of unassigned local variables, but not in the following case. Why?

private void Main()
{
    // This local variable is unassigned.
    string myVar;
    
    try
    {
        // The Throw() method prevents the assignment from happening...
        myVar = Throw();
    }
    finally
    {
        // ... so myVar is still unassigned.
    }

    // Here we use myVar, which is unassigned, and the compiler is not complaining.
    if (myVar.Equals("Something"))
    {
        // ...
    }
}

private string Throw()
{
    throw new Exception();
} 
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The exception in the example is not caught by Main(). Therefore, if the exception is thrown, the code after the finally block is unreachable, and if the exception is not thrown, the assignment occurs. That's why in any case a use of an unassigned variable cannot happen. And since it cannot happen, the compiler doesn't report it.

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