Humanizr/Humanizer · error · ArgumentException

Object is not a ByteSize

Error message

Object is not a ByteSize

What it means

Thrown by the non-generic ByteSize.CompareTo(object?) when the supplied object is neither null nor a ByteSize. Because ByteSize explicitly implements IComparable.CompareTo, this path is hit when the struct is compared through its boxed/object interface against an unrelated type.

Source

Thrown at src/Humanizer/Bytes/ByteSize.cs:1242

    public readonly bool Equals(ByteSize value) =>
        Bits == value.Bits;

    public readonly override int GetHashCode() =>
        Bits.GetHashCode();

    public readonly int CompareTo(object? obj)
    {
        if (obj == null)
        {
            return 1;
        }

        if (obj is ByteSize size)
        {
            return CompareTo(size);
        }

        throw new ArgumentException("Object is not a ByteSize");
    }

    public readonly int CompareTo(ByteSize other) =>
        Bits.CompareTo(other.Bits);

    public readonly ByteSize Add(ByteSize bs) =>
        new(Bytes + bs.Bytes);

    public readonly ByteSize AddBits(long value) =>
        this + FromBits(value);

    public readonly ByteSize AddBytes(double value) =>
        this + FromBytes(value);

    public readonly ByteSize AddKilobytes(double value) =>
        this + FromKilobytes(value);

    public readonly ByteSize AddMegabytes(double value) =>

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Compare ByteSize values through the generic IComparable<ByteSize> path or the strongly typed CompareTo(ByteSize).
  2. If you must use the object overload, type-check (obj is ByteSize) before calling and handle other types explicitly.
  3. Avoid mixing ByteSize with other types in non-generic collections; use List<ByteSize>.

Example fix

// before
var order = ((IComparable)sizeA).CompareTo(sizeBObject);

// after
if (sizeBObject is ByteSize sizeB)
{
    var order = sizeA.CompareTo(sizeB);
}
else
{
    throw new ArgumentException("Expected a ByteSize operand.", nameof(sizeBObject));
}
Defensive patterns

Strategy: type-guard

Validate before calling

static int SafeCompareTo(ByteSize a, object? b) =>
    b is ByteSize other ? a.CompareTo(other) : b is null ? 1 : throw new ArgumentException("Expected ByteSize.");

Type guard

static bool IsByteSize(object? o) => o is ByteSize;

Try / catch

try { return ((IComparable)a).CompareTo(b); }
catch (ArgumentException) { throw new ArgumentException("Operand must be a ByteSize.", nameof(b)); }

Prevention

When it happens

Trigger: Calling ((IComparable)byteSize).compareTo(someOtherType), sorting a heterogeneous ArrayList that mixes ByteSize with other values, or using a comparer that boxes operands and compares them as object.

Common situations: Binding ByteSize values into a UI grid that compares cells as object, serializing/deserializing into an ArrayList or non-generic collection, or a custom Comparison<object> that does not narrow first.

Related errors


AI-assisted analysis of Humanizr/Humanizer@ffc2b77c0f (2026-08-13). Data as JSON: /api/errors/63785c0c1c8aff76. Report an issue: GitHub.