{"record":{"id":"3554730b8148c66c","repo":"JamesNK/Newtonsoft.Json","slug":"wrapped-icollection-t-does-not-support-insert","errorCode":null,"errorMessage":"Wrapped ICollection<T> does not support Insert.","messagePattern":"Wrapped ICollection<T> does not support Insert\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs","lineNumber":226,"sourceCode":"\n            return -1;\n        }\n\n        void IList.RemoveAt(int index)\n        {\n            if (_genericCollection != null)\n            {\n                throw new InvalidOperationException(\"Wrapped ICollection<T> does not support RemoveAt.\");\n            }\n\n            _list!.RemoveAt(index);\n        }\n\n        void IList.Insert(int index, object? value)\n        {\n            if (_genericCollection != null)\n            {\n                throw new InvalidOperationException(\"Wrapped ICollection<T> does not support Insert.\");\n            }\n\n            VerifyValueType(value);\n            _list!.Insert(index, (T)value!);\n        }\n\n        bool IList.IsFixedSize\n        {\n            get\n            {\n                if (_genericCollection != null)\n                {\n                    // ICollection<T> only has IsReadOnly\n                    return _genericCollection.IsReadOnly;\n                }\n                else\n                {\n                    return _list!.IsFixedSize;","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs#L208-L244","documentation":"Thrown by CollectionWrapper<T>.IList.Insert when the underlying collection is an ICollection<T> that is not an IList, because insertion at a position requires index semantics that ICollection<T> does not provide.","triggerScenarios":"A CollectionWrapper wrapping an ICollection<T> (e.g. HashSet<T>) is asked to Insert(index, value). Reached during deserialization or via a custom converter/contract that drives positional insertion against a non-indexed collection.","commonSituations":"Deserializing JSON into a HashSet/set collection while a converter or the contract requests Insert; using CollectionWrapper directly against non-list collections; misconfigured collection contracts after refactoring.","solutions":["Back the wrapper with an IList<T> (List<T>) when positional insert is needed.","Use Add semantics (no position) instead of Insert for set-like collections.","Modify custom converters/contracts so they do not call Insert on ICollection<T>-only collections.","Choose target collection types that implement IList<T> for ordered insertion use cases."],"exampleFix":"// before: Insert on HashSet wrapper\nvar set = new HashSet<int>();\nvar wrapper = new CollectionWrapper<int>(set);\nwrapper.Insert(0, 1); // throws\n// after: List-backed wrapper supports Insert\nvar list = new List<int>();\nvar wrapper = new CollectionWrapper<int>(list);\nwrapper.Insert(0, 1);","handlingStrategy":"type-guard","validationCode":"if (!(collection is IList)) throw new NotSupportedException(\"Insert requires an IList\");","typeGuard":"static bool SupportsInsert<T>(ICollection<T> c) => c is IList;","tryCatchPattern":"try { ((IList)wrapper).Insert(0, item); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"does not support Insert\")) {\n    logger.Error(ex, \"underlying collection is not positional; use a List<T>.\"); throw;\n}","preventionTips":["Use List<T>-backed collections when positional insert is required.","Use Add for set-like/non-indexed collections.","Audit custom converters/contracts for Insert calls on ICollection<T>.","Choose IList-capable collection types for ordered insertion scenarios."],"tags":["collection-wrapper","icollection","notsupported","index-access"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}