Short answer: out of scope (scope)
This works:
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("¡Saludos Hermandad de SOes!");
DateTime fecha;
try
{
Console.Write("Fecha de nacimiento: ");
var nacimiento = Console.ReadLine();
fecha = Convert.ToDateTime(nacimiento);
Console.WriteLine(fecha);
}
catch (Exception)
{
Console.WriteLine("Error: El formato debe ser en dd/mm/aa");
}
}
}
This also works:
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("¡Saludos Hermandad de SOes!");
DateTime? fecha = null;
try
{
Console.Write("Fecha de nacimiento: ");
var nacimiento = Console.ReadLine();
fecha = Convert.ToDateTime(nacimiento);
}
catch (Exception)
{
Console.WriteLine("Error: El formato debe ser en dd/mm/aa");
}
Console.WriteLine(fecha);
}
}
The reason why this works is the "null" declaration of the "date", we achieve this thanks to the character "?".
You can define it in the following way as well:
Nullable<DateTime> fecha = null;
I recommend reading about null types: link