{"record":{"id":"7d7a95c4eef5ad4d","repo":"stride3d/stride","slug":"the-value","errorCode":null,"errorMessage":"The value \"","messagePattern":"The value \"","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core/Collections/SortedList.cs","lineNumber":616,"sourceCode":"        while (left <= right)\n        {\n            var guess = (left + right) >> 1;\n\n            var cmp = comparer.Compare(table[guess].Key, key);\n            if (cmp == 0) return guess;\n\n            if (cmp < 0) left = guess + 1;\n            else right = guess - 1;\n        }\n\n        return ~left;\n    }\n\n    private TKey ToKey(object key)\n    {\n        ArgumentNullException.ThrowIfNull(key);\n        if (key is not TKey key1)\n            throw new ArgumentException(\"The value \\\"\" + key + \"\\\" isn't of type \\\"\" + typeof(TKey) + \"\\\" and can't be used in this generic collection.\", nameof(key));\n        return key1;\n    }\n\n    private TValue ToValue(object value)\n    {\n        if (value is not TValue value1)\n            throw new ArgumentException(\"The value \\\"\" + value + \"\\\" isn't of type \\\"\" + typeof(TValue) + \"\\\" and can't be used in this generic collection.\", nameof(value));\n        return value1;\n    }\n\n    internal TKey KeyAt(int index)\n    {\n        if (index >= 0 && index < Count)\n            return table[index].Key;\n        throw new ArgumentOutOfRangeException(nameof(index));\n    }\n\n    internal TValue ValueAt(int index)","sourceCodeStart":598,"sourceCodeEnd":634,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/Collections/SortedList.cs#L598-L634","documentation":"ToKey is the type-coercion helper for non-generic IDictionary operations on SortedList. If the boxed key object cannot be cast to TKey, it throws ArgumentException explaining the value's type does not match the collection's key type. It first rejects null keys via ArgumentNullException.","triggerScenarios":"Calling non-generic IDictionary.Add/Contains/Remove (or indexer get/set) with an object that is not TKey, e.g. dict.Add(5, \"x\") on a SortedList<string,T> boxed as IDictionary.","commonSituations":"Legacy non-generic IDictionary interop, reflection-based configuration loaders reading values from XML/JSON as strings, data-binding frameworks passing untyped objects.","solutions":["Pass keys already of type TKey; parse/convert first (e.g. int.Parse(raw)).","Use the generic SortedList API directly (Add(TKey, TValue), ContainsKey(TKey)) to get compile-time type safety.","If the type is dynamic, check key is TKey before invoking the non-generic API.","Use Convert.ChangeType with invariant culture when raw data is a string representation."],"exampleFix":"// before\n((IDictionary)sl).Add(rawValue, value); // rawValue is object/string\n// after\nvar key = (TKey)Convert.ChangeType(rawValue, typeof(TKey), CultureInfo.InvariantCulture);\nsl.Add(key, value);","handlingStrategy":"type-guard","validationCode":"if (key == null) throw new ArgumentNullException(nameof(key));\nif (key is not TKey typedKey)\n    typedKey = (TKey)Convert.ChangeType(key, typeof(TKey), CultureInfo.InvariantCulture);\nsortedList.Add(typedKey, value);","typeGuard":"static bool IsTypedKey(object key) => key is TKey;","tryCatchPattern":"try { ((IDictionary)sortedList).Add(key, value); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"isn't of type\"))\n{ /* convert key to TKey and retry via the generic API */ }","preventionTips":["Use the generic SortedList API exclusively to keep type safety at compile time.","Parse strings from config/JSON into TKey before insertion.","Pattern-match object inputs (key is TKey k) at interop boundaries.","Avoid the non-generic IDictionary cast unless a legacy API requires it."],"tags":["collections","type-mismatch","generic-collections"],"backgroundTag":"type-mismatch","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}