Object - ToNullableSByteOrDefault

An object extension method that converts this object to a nullable s byte or default.

Try it

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

View Source
using System;

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

            return Convert.ToSByte(@this);
        }
        catch (Exception)
        {
            return default(sbyte);
        }
    }

    /// <summary>
    ///     An object extension method that converts this object to a nullable s byte 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 a sbyte?</returns>
    public static sbyte? ToNullableSByteOrDefault(this object @this, sbyte? defaultValue)
    {
        try
        {
            if (@this == null || @this == DBNull.Value)
            {
                return null;
            }

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

    /// <summary>
    ///     An object extension method that converts this object to a nullable s byte 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 a sbyte?</returns>
    public static sbyte? ToNullableSByteOrDefault(this object @this, Func<sbyte?> defaultValueFactory)
    {
        try
        {
            if (@this == null || @this == DBNull.Value)
            {
                return null;
            }

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