{"record":{"id":"63785c0c1c8aff76","repo":"Humanizr/Humanizer","slug":"object-is-not-a-bytesize","errorCode":null,"errorMessage":"Object is not a ByteSize","messagePattern":"Object is not a ByteSize","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Humanizer/Bytes/ByteSize.cs","lineNumber":1242,"sourceCode":"    public readonly bool Equals(ByteSize value) =>\n        Bits == value.Bits;\n\n    public readonly override int GetHashCode() =>\n        Bits.GetHashCode();\n\n    public readonly int CompareTo(object? obj)\n    {\n        if (obj == null)\n        {\n            return 1;\n        }\n\n        if (obj is ByteSize size)\n        {\n            return CompareTo(size);\n        }\n\n        throw new ArgumentException(\"Object is not a ByteSize\");\n    }\n\n    public readonly int CompareTo(ByteSize other) =>\n        Bits.CompareTo(other.Bits);\n\n    public readonly ByteSize Add(ByteSize bs) =>\n        new(Bytes + bs.Bytes);\n\n    public readonly ByteSize AddBits(long value) =>\n        this + FromBits(value);\n\n    public readonly ByteSize AddBytes(double value) =>\n        this + FromBytes(value);\n\n    public readonly ByteSize AddKilobytes(double value) =>\n        this + FromKilobytes(value);\n\n    public readonly ByteSize AddMegabytes(double value) =>","sourceCodeStart":1224,"sourceCodeEnd":1260,"githubUrl":"https://github.com/Humanizr/Humanizer/blob/ffc2b77c0f30d2fb176875841424379319d0ae9b/src/Humanizer/Bytes/ByteSize.cs#L1224-L1260","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Compare ByteSize values through the generic IComparable<ByteSize> path or the strongly typed CompareTo(ByteSize).","If you must use the object overload, type-check (obj is ByteSize) before calling and handle other types explicitly.","Avoid mixing ByteSize with other types in non-generic collections; use List<ByteSize>."],"exampleFix":"// before\nvar order = ((IComparable)sizeA).CompareTo(sizeBObject);\n\n// after\nif (sizeBObject is ByteSize sizeB)\n{\n    var order = sizeA.CompareTo(sizeB);\n}\nelse\n{\n    throw new ArgumentException(\"Expected a ByteSize operand.\", nameof(sizeBObject));\n}","handlingStrategy":"type-guard","validationCode":"static int SafeCompareTo(ByteSize a, object? b) =>\n    b is ByteSize other ? a.CompareTo(other) : b is null ? 1 : throw new ArgumentException(\"Expected ByteSize.\");","typeGuard":"static bool IsByteSize(object? o) => o is ByteSize;","tryCatchPattern":"try { return ((IComparable)a).CompareTo(b); }\ncatch (ArgumentException) { throw new ArgumentException(\"Operand must be a ByteSize.\", nameof(b)); }","preventionTips":["Prefer the generic IComparable<ByteSize> path","Type-check before the object CompareTo overload","Avoid non-generic collections holding ByteSize"],"tags":["bytesize","type-mismatch","icomparable","argumentexception"],"backgroundTag":null,"analyzedSha":"ffc2b77c0f30d2fb176875841424379319d0ae9b","analyzedAt":"2026-08-13T21:42:34.584Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}