using System;
namespace Articulos.Cap04.TiposNullables
{
public sealed class IntroTiposNullables
{
public static void Main()
{
// Declaración implícita de un tipo nullable entero
// de 32 bits:
int? numero = null;
// Comprobamos si la variable `numero` contiene un
// valor diferente a null asignado:
if (numero.HasValue)
{
Console.WriteLine ("Valor de `numero`: {0}", numero.Value.ToString());
}
else
{
Console.WriteLine ("`numero` = null");
}
// Asignamos el valor por defecto dado para un
// entero de 32 bits:
int y = numero.GetValueOrDefault();
// El intento de obtener el valor de la variable
// entera `numero`, la cual no cuenta con un valor
// distinto de null genera la excepción
// InvalidOperationException:
try
{
y = numero.Value;
}
catch (InvalidOperationException e)
{
Console.WriteLine ("Mensaje de excepción: {0}", e.Message);
}
}
}
}
dXNpbmcgU3lzdGVtOwoKbmFtZXNwYWNlIEFydGljdWxvcy5DYXAwNC5UaXBvc051bGxhYmxlcwp7CiAgICBwdWJsaWMgc2VhbGVkIGNsYXNzIEludHJvVGlwb3NOdWxsYWJsZXMKICAgIHsKICAgICAgICBwdWJsaWMgc3RhdGljIHZvaWQgTWFpbigpCiAgICAgICAgewogICAgICAgICAgICAvLyBEZWNsYXJhY2nDs24gaW1wbMOtY2l0YSBkZSB1biB0aXBvIG51bGxhYmxlIGVudGVybyAKICAgICAgICAgICAgLy8gZGUgMzIgIGJpdHM6CiAgICAgICAgICAgIGludD8gbnVtZXJvID0gbnVsbDsKICAgICAgICAgICAgCiAgICAgICAgICAgIC8vIENvbXByb2JhbW9zIHNpIGxhIHZhcmlhYmxlIGBudW1lcm9gIGNvbnRpZW5lIHVuIAogICAgICAgICAgICAvLyB2YWxvciBkaWZlcmVudGUgYSBudWxsIGFzaWduYWRvOgogICAgICAgICAgICBpZiAobnVtZXJvLkhhc1ZhbHVlKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSAoIlZhbG9yIGRlIGBudW1lcm9gOiB7MH0iLCBudW1lcm8uVmFsdWUuVG9TdHJpbmcoKSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSAoImBudW1lcm9gID0gbnVsbCIpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIAogICAgICAgICAgICAvLyBBc2lnbmFtb3MgZWwgdmFsb3IgcG9yIGRlZmVjdG8gZGFkbyBwYXJhIHVuIAogICAgICAgICAgICAvLyBlbnRlcm8gZGUgMzIgYml0czoKICAgICAgICAgICAgaW50IHkgPSBudW1lcm8uR2V0VmFsdWVPckRlZmF1bHQoKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIC8vIEVsIGludGVudG8gZGUgb2J0ZW5lciBlbCB2YWxvciBkZSBsYSB2YXJpYWJsZSAKICAgICAgICAgICAgLy8gZW50ZXJhIGBudW1lcm9gLCBsYSBjdWFsIG5vIGN1ZW50YSBjb24gdW4gdmFsb3IgCiAgICAgICAgICAgIC8vIGRpc3RpbnRvIGRlIG51bGwgZ2VuZXJhIGxhIGV4Y2VwY2nDs24gCiAgICAgICAgICAgIC8vIEludmFsaWRPcGVyYXRpb25FeGNlcHRpb246CiAgICAgICAgICAgIHRyeQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICB5ID0gbnVtZXJvLlZhbHVlOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGNhdGNoIChJbnZhbGlkT3BlcmF0aW9uRXhjZXB0aW9uIGUpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lICgiTWVuc2FqZSBkZSBleGNlcGNpw7NuOiB7MH0iLCBlLk1lc3NhZ2UpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9