{"record":{"id":"f2a74d9e392857b2","repo":"JamesNK/Newtonsoft.Json","slug":"set-jconstructor-values-with-invalid-key-value-0","errorCode":null,"errorMessage":"Set JConstructor values with invalid key value: {0}. Argument position index expected.","messagePattern":"Set JConstructor values with invalid key value: (.+?)\\. Argument position index expected\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JConstructor.cs","lineNumber":205,"sourceCode":"        {\n            get\n            {\n                ValidationUtils.ArgumentNotNull(key, nameof(key));\n\n                if (!(key is int i))\n                {\n                    throw new ArgumentException(\"Accessed JConstructor values with invalid key value: {0}. Argument position index expected.\".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));\n                }\n\n                return GetItem(i);\n            }\n            set\n            {\n                ValidationUtils.ArgumentNotNull(key, nameof(key));\n\n                if (!(key is int i))\n                {\n                    throw new ArgumentException(\"Set JConstructor values with invalid key value: {0}. Argument position index expected.\".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));\n                }\n\n                SetItem(i, value);\n            }\n        }\n\n        internal override int GetDeepHashCode()\n        {\n            int hash;\n#if HAVE_GETHASHCODE_STRING_COMPARISON\n            hash = _name?.GetHashCode(StringComparison.Ordinal) ?? 0;\n#else\n            hash = _name?.GetHashCode() ?? 0;\n#endif\n            return hash ^ ContentsHashCode();\n        }\n\n        /// <summary>","sourceCodeStart":187,"sourceCodeEnd":223,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JConstructor.cs#L187-L223","documentation":"Thrown by the JConstructor[object key] setter when assigning with a non-int key. Like the getter, JConstructor arguments are positional.","triggerScenarios":"Assigning myCtor[\"x\"] = token or using a non-int boxed key on the set side.","commonSituations":"Mutating constructor arguments with code patterned after JObject; dynamic key types.","solutions":["Assign via the integer position: myCtor[index] = value.","Rebuild the JConstructor with the desired arguments rather than keyed assignment.","Guard the key type before assignment."],"exampleFix":"// before\nmyCtor[(object)key] = new JValue(1);\n// after\nmyCtor[0] = new JValue(1);","handlingStrategy":"type-guard","validationCode":"if (key is int i) { ctor[i] = value; }\nelse throw new InvalidOperationException(\"JConstructor setter requires an int argument index.\");","typeGuard":"static bool IsCtorKey(object key) => key is int;","tryCatchPattern":"try { ctor[key] = value; }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Argument position index expected\")) {\n    // coerce key to int\n}","preventionTips":["Assign JConstructor arguments by integer position only.","Rebuild the JConstructor if you need structural change.","Keep dynamic-keyed mutation out of JConstructor code."],"tags":["jconstructor","indexer","argument"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}