{"record":{"id":"6fb2321b4a9d1e24","repo":"clockworklabs/SpacetimeDB","slug":"argument-must-be-a-u128","errorCode":null,"errorMessage":"Argument must be a U128","messagePattern":"Argument must be a U128","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"crates/bindings-csharp/BSATN.Runtime/BSATN/U128.cs","lineNumber":77,"sourceCode":"        BinaryPrimitives.WriteUInt64BigEndian(bytes.AsSpan(8, 8), _lower);\n\n        return bytes;\n    }\n\n    /// <inheritdoc cref=\"IComparable.CompareTo(object)\" />\n    public int CompareTo(object? value)\n    {\n        if (value is U128 other)\n        {\n            return CompareTo(other);\n        }\n        else if (value is null)\n        {\n            return 1;\n        }\n        else\n        {\n            throw new ArgumentException(\"Argument must be a U128\", nameof(value));\n        }\n    }\n\n    /// <inheritdoc cref=\"IComparable{T}.CompareTo(T)\" />\n    public int CompareTo(U128 value)\n    {\n        if (this < value)\n        {\n            return -1;\n        }\n        else if (this > value)\n        {\n            return 1;\n        }\n        else\n        {\n            return 0;\n        }","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/bindings-csharp/BSATN.Runtime/BSATN/U128.cs#L59-L95","documentation":"U128 is the SpacetimeDB BSATN.Runtime 128-bit unsigned integer. The untyped IComparable.CompareTo(object?) overload accepts only a U128 or null; any other type throws ArgumentException since cross-type ordering is undefined.","triggerScenarios":"Calling CompareTo(object?) with a boxed long, I128, U256, Guid, or other type — via Array.Sort on object[], ArrayList, or manual object comparisons.","commonSituations":"Sorting heterogeneous id collections; comparing Uuids or ids pulled out as object; dynamic code paths that box numerics before comparing.","solutions":["Use the typed CompareTo(U128) overload or the <, >, == operators","Pattern-match (if (o is U128 x)) before comparing boxed values","Keep sorted collections typed as U128[] instead of object[]"],"exampleFix":"// before\nint cmp = u128.CompareTo(boxedLong); // ArgumentException: Argument must be a U128\n\n// after\nif (boxedLong is U128 other)\n{\n    int cmp = u128.CompareTo(other);\n}","handlingStrategy":"type-guard","validationCode":"static bool CanCompareWithU128(object? other) => other is null || other is SpacetimeDB.U128;","typeGuard":"static bool IsU128(object? value) => value is SpacetimeDB.U128;","tryCatchPattern":"try\n{\n    var cmp = ((IComparable)u128Value).CompareTo(boxed);\n}\ncatch (ArgumentException)\n{\n    // convert operand or reject the mixed comparison\n}","preventionTips":["Use CompareTo(U128) or the comparison operators","Keep id collections typed","Pattern-match before object-based comparisons"],"tags":["csharp","spacetimedb","bsatn","uint128","icomparable"],"backgroundTag":"icomparable-type-mismatch","analyzedSha":"6dee26c6efc2856793e12b148a59742964f5d783","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}