{"record":{"id":"6abc624b0b2d7aca","repo":"reactiveui/refit","slug":"enum-typeof-tenum-does-not-use-a-signed-backing","errorCode":null,"errorMessage":"Enum {typeof(TEnum)} does not use a signed backing type.","messagePattern":"Enum (.+?) does not use a signed backing type\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"src/Refit/EnumHelpers.cs","lineNumber":150,"sourceCode":"                writer.WriteNumberValue(ToUInt64(value));\n                return;\n            }\n\n            writer.WriteNumberValue(ToInt64(value));\n        }\n\n        /// <summary>Converts an enum value to a signed 64-bit number without boxing.</summary>\n        /// <param name=\"value\">The enum value.</param>\n        /// <returns>The signed numeric value.</returns>\n        /// <exception cref=\"JsonException\"><typeparamref name=\"TEnum\"/> is backed by an unsigned type, so it has no signed representation.</exception>\n        internal static long ToInt64(TEnum value) =>\n            _underlyingTypeCode switch\n            {\n                TypeCode.SByte => Unsafe.As<TEnum, sbyte>(ref value),\n                TypeCode.Int16 => Unsafe.As<TEnum, short>(ref value),\n                TypeCode.Int32 => Unsafe.As<TEnum, int>(ref value),\n                TypeCode.Int64 => Unsafe.As<TEnum, long>(ref value),\n                _ => throw new JsonException($\"Enum {typeof(TEnum)} does not use a signed backing type.\")\n            };\n\n        /// <summary>Converts an enum value to an unsigned 64-bit number without boxing.</summary>\n        /// <param name=\"value\">The enum value.</param>\n        /// <returns>The unsigned numeric value.</returns>\n        /// <exception cref=\"JsonException\"><typeparamref name=\"TEnum\"/> is backed by a signed type, so it has no unsigned representation.</exception>\n        internal static ulong ToUInt64(TEnum value) =>\n            _underlyingTypeCode switch\n            {\n                TypeCode.Byte => Unsafe.As<TEnum, byte>(ref value),\n                TypeCode.UInt16 => Unsafe.As<TEnum, ushort>(ref value),\n                TypeCode.UInt32 => Unsafe.As<TEnum, uint>(ref value),\n                TypeCode.UInt64 => Unsafe.As<TEnum, ulong>(ref value),\n                _ => throw new JsonException($\"Enum {typeof(TEnum)} does not use an unsigned backing type.\")\n            };\n\n        /// <summary>Converts a numeric backing value to the enum type without boxing.</summary>\n        /// <typeparam name=\"TUnderlying\">The enum backing value type.</typeparam>","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/reactiveui/refit/blob/b455f65ecc4c97d092317e349cb775f9cfc6bcdf/src/Refit/EnumHelpers.cs#L132-L168","documentation":"Thrown by EnumHelpers.ToInt64 when converting an enum value to a signed 64-bit number but the enum's underlying type is unsigned (byte/ushort/uint/ulong), which has no lossless signed representation in that path. The switch only covers the signed TypeCodes; an unsigned enum falls to the default arm.","triggerScenarios":"ToInt64 is invoked for an enum whose underlying type is unsigned (e.g. enum E : ulong). This is an internal serialization-path method; a well-formed caller would route unsigned enums through ToUInt64 instead. Reaching the throw means the wrong signedness helper was selected for the enum.","commonSituations":"Effectively unreachable through normal Refit usage because the serialization code checks IsUnsignedBackingType and calls the matching helper. It can surface only if custom code calls EnumHelpers.Info<TEnum>.ToInt64 directly on an unsigned-backed enum, or due to an internal bug.","solutions":["Do not call ToInt64 on an unsigned-backed enum — use ToUInt64, or check EnumHelpers.IsUnsignedBackingType first.","If encountered through normal use (no custom EnumHelpers usage), report it as a Refit internal bug with the enum definition.","Consider switching the enum to a signed underlying type if a signed numeric form is genuinely required."],"exampleFix":"// before — calling the signedness-mismatched helper on a ulong enum\npublic enum Flags : ulong { A, B }\nlong v = EnumHelpers.Info<Flags>.ToInt64(flags); // throws\n\n// after — use the matching unsigned helper\nulong v = EnumHelpers.Info<Flags>.ToUInt64(flags);","handlingStrategy":"validation","validationCode":"// Route by signedness instead of assuming a signed representation exists.\nstatic long SafeToInt64<TEnum>(TEnum value) where TEnum : struct, Enum =>\n    EnumHelpers.Info<TEnum>.IsUnsignedBackingType(EnumHelpers.Info<TEnum>.UnderlyingTypeCode)\n        ? throw new InvalidOperationException(\"Enum is unsigned; use ToUInt64.\")\n        : EnumHelpers.Info<TEnum>.ToInt64(value);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check the enum's backing signedness (IsUnsignedBackingType) before choosing ToInt64 vs ToUInt64.","Don't call internal EnumHelpers helpers directly in app code; prefer Enum.ToObject/Convert.","If it surfaces during normal Refit use, report it as an internal bug."],"tags":["enum","json","enum-helpers","internal","signedness"],"backgroundTag":null,"analyzedSha":"b455f65ecc4c97d092317e349cb775f9cfc6bcdf","analyzedAt":"2026-08-13T21:20:57.878Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}