{"record":{"id":"3245da3c8477dd03","repo":"dotnet/wpf","slug":"throw-new-argumentoutofrangeexception-nameof-index-3245da","errorCode":null,"errorMessage":"throw new ArgumentOutOfRangeException(nameof(index));","messagePattern":"throw new ArgumentOutOfRangeException\\(nameof\\(index\\)\\);","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Utility/FrugalList.cs","lineNumber":285,"sourceCode":"            if (EqualityComparer<T>.Default.Equals(_loneEntry, value))\n            {\n                return 0;\n            }\n\n            return -1;\n        }\n\n        public override void Insert(int index, T value)\n        {\n            // Should only get here if count and index are 0\n            if ((_count < SIZE) && (index < SIZE))\n            {\n                _loneEntry = value;\n                ++_count;\n                return;\n            }\n\n            throw new ArgumentOutOfRangeException(nameof(index));\n        }\n\n        public override void SetAt(int index, T value)\n        {\n            // Overwrite item at index\n            _loneEntry = value;\n        }\n\n        public override bool Remove(T value)\n        {\n            // Wipe out the info in the only entry if it matches the item.\n            if (EqualityComparer<T>.Default.Equals(_loneEntry, value))\n            {\n                _loneEntry = default(T);\n                --_count;\n                return true;\n            }\n","sourceCodeStart":267,"sourceCodeEnd":303,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Utility/FrugalList.cs#L267-L303","documentation":"FrugalList's SingleItemList.Insert throws ArgumentOutOfRangeException when Insert is called with an index other than 0 on an empty/single-entry list. A frugal single-item list only ever holds one entry, so the only legal insert position is index 0 when _count is 0; any other index is out of range for the backing store.","triggerScenarios":"Calling Insert(index, value) with index != 0 on a SingleItemList (count 0 or 1), e.g. Insert(1, item) on a fresh list, or Insert at the end position when count is already 1.","commonSituations":"Generic code written against IList that appends with Insert(Count, item) — for a single-item list Count may be 0 and Insert(0,...) works, but code that assumes larger capacity and computes indices from a stale count will land here.","solutions":["Only insert at index 0 into a single-item frugal list","Use Add instead of Insert for appending; the frugal list promotes capacity automatically","Check that the index satisfies 0 <= index <= _count before calling Insert","If multiple items are needed, upgrade to a larger FrugalList variant or a standard List<T>"],"exampleFix":"// before\nlist.Insert(list.Count + 1, item); // wrong index\n// after\nlist.Insert(0, item); // or list.Add(item);","handlingStrategy":"validation","validationCode":"if (index < 0 || index > list.Count) throw new ArgumentOutOfRangeException(nameof(index));","typeGuard":null,"tryCatchPattern":"try { list.Insert(index, item); }\ncatch (ArgumentOutOfRangeException) { list.Add(item); // fall back to append\n}","preventionTips":["Use Add instead of Insert for appends","Only insert at 0 in a single-item frugal list","Re-derive index from list.Count immediately before inserting"],"tags":["wpf","collections","argument-out-of-range"],"backgroundTag":"argument-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}