Object - ToNullableUInt16OrDefault

An object extension method that converts this object to a nullable u int 16 or default.

Try it

public static void Main()
{
	object val1 = 123;
    object val2 = "1090";
	object thisInvalid = "A100";
	object thisNull = null;
	
	string format = "{0, -30} {1, -15}";
	
	Console.WriteLine(format, "Object", "ToNullableUInt16OrDefault");
	Console.WriteLine(format, "------", "-------------------------");
	
	// C# Extension Method: Object - ToNullableUInt16OrDefault
	Console.WriteLine(format, val1, val1.ToNullableUInt16OrDefault());
	Console.WriteLine(format, val2, val2.ToNullableUInt16OrDefault());
	Console.WriteLine(format, "null", thisNull.ToNullableUInt16OrDefault());
	Console.WriteLine(format, thisInvalid, thisInvalid.ToNullableUInt16OrDefault());
	Console.WriteLine(format, thisInvalid, thisInvalid.ToNullableUInt16OrDefault(0));
	Console.WriteLine(format, thisInvalid, thisInvalid.ToNullableUInt16OrDefault(() => 0));
}

View Source
using System;

public static partial class Extensions
{
    /// <summary>
    ///     An object extension method that converts this object to a nullable u int 16 or default.
    /// </summary>
    /// <param name="this">The @this to act on.</param>
    /// <returns>The given data converted to an ushort?</returns>
    public static ushort? ToNullableUInt16OrDefault(this object @this)
    {
        try
        {
            if (@this == null || @this == DBNull.Value)
            {
                return null;
            }

            return Convert.ToUInt16(@this);
        }
        catch (Exception)
        {
            return default(ushort);
        }
    }

    /// <summary>
    ///     An object extension method that converts this object to a nullable u int 16 or default.
    /// </summary>
    /// <param name="this">The @this to act on.</param>
    /// <param name="defaultValue">The default value.</param>
    /// <returns>The given data converted to an ushort?</returns>
    public static ushort? ToNullableUInt16OrDefault(this object @this, ushort? defaultValue)
    {
        try
        {
            if (@this == null || @this == DBNull.Value)
            {
                return null;
            }

            return Convert.ToUInt16(@this);
        }
        catch (Exception)
        {
            return defaultValue;
        }
    }

    /// <summary>
    ///     An object extension method that converts this object to a nullable u int 16 or default.
    /// </summary>
    /// <param name="this">The @this to act on.</param>
    /// <param name="defaultValueFactory">The default value factory.</param>
    /// <returns>The given data converted to an ushort?</returns>
    public static ushort? ToNullableUInt16OrDefault(this object @this, Func<ushort?> defaultValueFactory)
    {
        try
        {
            if (@this == null || @this == DBNull.Value)
            {
                return null;
            }

            return Convert.ToUInt16(@this);
        }
        catch (Exception)
        {
            return defaultValueFactory();
        }
    }
}