C# Extension Method: Object - ToNullableInt64

CSharp · Console

/Program.cs

// C# Extension Method
// Doc: https://csharp-extension.com/en/method/1002520/object-tonullableint64

// @nuget: Z.ExtensionMethods

using System;
					
public class Program
{
	public static void Main()
	{
        object[] values = { true, null, -12, 163, 935, 'x', new DateTime(2009, 5, 12),
                "104", "103.0", "-1",
                "1.00e2", "One", 1.00e2, 16.3e42};
        long? result;

        foreach (object value in values)
        {
            try
            {
				//C# Extension Method: Object - ToNullableInt64
                result = value.ToNullableInt64();
                Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
                                  value == null ? "null" : value.GetType().Name,
								  value == null ? "null" : value,
								  result == null ? "null" : result.GetType().Name,
								  result == null ? "null" : result.ToString());
            }
            catch (OverflowException)
            {
                Console.WriteLine("The {0} value {1} is outside the range of the long type.",
                                  value == null ? "null" : value.GetType().Name, value);
            }
            catch (FormatException)
            {
                Console.WriteLine("The {0} value {1} is not in a recognizable format.",
                                  value == null ? "null" : value.GetType().Name, value);
            }
            catch (InvalidCastException)
            {
                Console.WriteLine("No conversion to an long exists for the {0} value {1}.",
                                  value == null ? "null" : value.GetType().Name, value);

            }
        }
	}
}
;