Object - IsValidByte

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

Try it

public static void Main()
{
	string format = "{0, -10} {1, -15}";
	object [] values = { "-128" ,"12.8", "-89", "ABC", "true", "128", "a", ".", "0", "10.2m"};
	
	Console.WriteLine(format, "Object", "Is Valid Byte");
	Console.WriteLine(format, "------", "-------------");
	
	foreach(var val in values)
	{
		// C# Extension Method: Object - IsValidByte
		Console.WriteLine(format, val, val.IsValidByte());
	}
}

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

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