CSharp · Console
// C# Extension Method
// Doc: https://csharp-extension.com/en/method/1002500/object-tonullablebyte
// @nuget: Z.ExtensionMethods
using System;
public class Program
{
public static void Main()
{
object[] values = { null, true, -12, 163, 935, 'x', "104", "103.0", "-1",
"1.00e2", "One", 1.00e2};
foreach (object value in values)
{
try
{
// C# Extension Method: Object - ToNullableByte
byte? result = value.ToNullableByte();
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
value != null ? value.GetType().Name : "null", value,
result != null ? result.GetType().Name : "null", result);
}
catch (OverflowException)
{
Console.WriteLine("The {0} value {1} is outside the range of the Byte type.",
value.GetType().Name, value);
}
catch (FormatException)
{
Console.WriteLine("The {0} value {1} is not in a recognizable format.",
value.GetType().Name, value);
}
catch (InvalidCastException)
{
Console.WriteLine("No conversion to a Byte exists for the {0} value {1}.",
value.GetType().Name, value);
}
}
}
}