{"record":{"id":"70821cc08e26e829","repo":"JamesNK/Newtonsoft.Json","slug":"wrapped-icollection-t-does-not-support-indexof","errorCode":null,"errorMessage":"Wrapped ICollection<T> does not support IndexOf.","messagePattern":"Wrapped ICollection<T> does not support IndexOf\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs","lineNumber":201,"sourceCode":"\n            return (Count - 1);\n        }\n\n        bool IList.Contains(object? value)\n        {\n            if (IsCompatibleObject(value))\n            {\n                return Contains((T)value!);\n            }\n\n            return false;\n        }\n\n        int IList.IndexOf(object? value)\n        {\n            if (_genericCollection != null)\n            {\n                throw new InvalidOperationException(\"Wrapped ICollection<T> does not support IndexOf.\");\n            }\n\n            if (IsCompatibleObject(value))\n            {\n                return _list!.IndexOf((T)value!);\n            }\n\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);","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs#L183-L219","documentation":"Thrown by CollectionWrapper<T>.IList.IndexOf when the wrapper was constructed around an ICollection<T> (which has no positional/index semantics) rather than an IList. The wrapper implements IList but only delegates index operations when the underlying collection is an IList; an ICollection<T> cannot honor IndexOf.","triggerScenarios":"Json.NET internally (during collection deserialization or serialization) calls IList.IndexOf on a CollectionWrapper that wraps a HashSet<T>, Dictionary<T>.ValueCollection, or any other ICollection<T> that is not an IList. Typically reached via a custom JsonConverter or contract that exposes such a collection where index access is attempted.","commonSituations":"Deserializing into or serializing a HashSet<T>/ISet<T>/KeyValuePair collection that a converter or custom contract treats as a list; using a CollectionWrapper directly in user code against a non-indexed collection; rare and usually surfaces from custom collection contracts.","solutions":["Avoid treating ICollection<T>-only collections (HashSet, set-like) as ordered/indexed collections.","If positional access is required, materialize the data into a List<T> first.","Adjust any custom JsonConverter/contract so it does not request index-based operations on non-IList collections.","Use JsonSerializer with collection types that implement IList<T> when index access is needed."],"exampleFix":"// before: HashSet wrapped where index access attempted\nvar set = new HashSet<int>{1,2,3};\nvar wrapper = new CollectionWrapper<int>(set);\nwrapper.IndexOf(2); // throws\n// after: use a List for positional access\nvar list = new List<int>{1,2,3};\nvar wrapper = new CollectionWrapper<int>(list);","handlingStrategy":"type-guard","validationCode":"if (!(collection is IList)) throw new NotSupportedException(\"IndexOf requires an IList\");","typeGuard":"static bool SupportsIndex<T>(ICollection<T> c) => c is IList;","tryCatchPattern":"try { var i = ((IList)wrapper).IndexOf(item); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"does not support IndexOf\")) {\n    logger.Error(ex, \"underlying collection is not positional; use a List<T>.\"); throw;\n}","preventionTips":["Use List<T> (IList) backed collections when positional access is required.","Avoid index operations on HashSet/set-like collections.","Materialize non-indexed collections into List<T> before positional work.","Audit custom converters that may call IndexOf on arbitrary collections."],"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"}