Object - IsValidSByte

An object extension method that query if '@this' is valid sbyte.

Try it

public static void Main()
{
	string format = "{0, -10} {1, -15}";
	object [] values = {"-3.6", "12.8", "+16.7", "    3   ", "(17)", 
                        "-17", "+12", "18-", "987", "1,024", "  127 "};
	
	Console.WriteLine(format, "Object", "Is Valid SByte");
	Console.WriteLine(format, "------", "--------------");
	
	foreach(var val in values)
	{
		// C# Extension Method: Object - IsValidSByte
		Console.WriteLine(format, val, val.IsValidSByte());
	}
}

View Source
public static partial class Extensions
{
    /// <summary>
    ///     An object extension method that query if '@this' is valid sbyte.
    /// </summary>
    /// <param name="this">The @this to act on.</param>
    /// <returns>true if valid sbyte, false if not.</returns>
    public static bool IsValidSByte(this object @this)
    {
        if (@this == null)
        {
            return true;
        }

        sbyte result;
        return sbyte.TryParse(@this.ToString(), out result);
    }
}