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

271
Vistas
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 Respuestas
Responde la pregunta

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 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