public static void Main()
object[] values = { true, 'a', 123, 1.764e32, "9.78", "1e-02",
1.67e03, "A100", "1,033.67", DateTime.Now,
foreach (object value in values)
decimal result = value.ToDecimal();
Console.WriteLine("Converted the {0} value {1} to {2}.",
value.GetType().Name, value, result);
catch (OverflowException)
Console.WriteLine("The {0} value {1} is out of range of the Decimal type.",
value.GetType().Name, value);
Console.WriteLine("The {0} value {1} is not recognized as a valid Decimal value.",
value.GetType().Name, value);
catch (InvalidCastException)
Console.WriteLine("Conversion of the {0} value {1} to a Decimal is not supported.",
value.GetType().Name, value);