{"record":{"id":"71aa8cfa546930b1","repo":"dotnet/wpf","slug":"throw-new-argumentoutofrangeexception-nameof-index-71aa8c","errorCode":null,"errorMessage":"throw new ArgumentOutOfRangeException(nameof(index)); // validating the index argument","messagePattern":"throw new ArgumentOutOfRangeException\\(nameof\\(index\\)\\); // validating the index argument","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/IndexedEnumerable.cs","lineNumber":243,"sourceCode":"                    if (index == _cachedIndex)\n                        return _cachedItem;\n                }\n                else\n                {\n                    // there are new enumerators, caches were cleared, recalculate moveBy\n                    moveBy = index + 1; // force at least one MoveNext\n                }\n\n                // position enumerator at new index:\n                while ((moveBy > 0) && _enumerator.MoveNext())\n                {\n                    moveBy--;\n                }\n\n                // moved beyond the end of the enumerator?\n                if (moveBy != 0)\n                {\n                    throw new ArgumentOutOfRangeException(nameof(index)); // validating the index argument\n                }\n\n                CacheCurrentItem(index, _enumerator.Current);\n                return _cachedItem;\n            }\n        }\n\n        /// <summary>\n        /// The enumerable collection wrapped by this indexer.\n        /// </summary>\n        internal IEnumerable Enumerable\n        {\n            get { return _enumerable; }\n        }\n\n        /// <summary>\n        /// The collection wrapped by this indexer.\n        /// </summary>","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/IndexedEnumerable.cs#L225-L261","documentation":"IndexedEnumerable.GetItemAt(index) uses a live enumerator: it calls MoveNext 'index' times to reach the requested item. If the enumerator runs out before consuming 'index' moves (moveBy != 0 at the end), the index exceeds the number of available items and ArgumentOutOfRangeException is thrown for the 'index' parameter.","triggerScenarios":"Calling IndexedEnumerable.GetItemAt(index) (public API) with index >= Count of the underlying enumerable, or with an index valid at construction but beyond the (possibly changed/shrunken) source.","commonSituations":"ItemsControl scrolling code asking for an item at a position that no longer exists after the source collection shrank; off-by-one loops using Count from a stale copy.","solutions":["Check index against Count (or InternalCount) before calling GetItemAt.","Re-create the IndexedEnumerable if the underlying collection changed size.","Use TryGetItemAt-style logic by catching ArgumentOutOfRangeException if racing changes are expected.","Clamp index to Count - 1."],"exampleFix":"// before\nvar item = indexedEnumerable.GetItemAt(i);\n// after\nif (i < indexedEnumerable.Count)\n    var item = indexedEnumerable.GetItemAt(i);","handlingStrategy":"validation","validationCode":"if (index < 0 || index >= indexedEnumerable.Count)\n    throw new ArgumentOutOfRangeException(nameof(index), \"index exceeds available items\");\nvar item = indexedEnumerable.GetItemAt(index);","typeGuard":"bool IsValidIndex(IndexedEnumerable e, int i) => i >= 0 && i < e.Count;","tryCatchPattern":"try { item = e.GetItemAt(i); }\ncatch (ArgumentOutOfRangeException) { item = null; /* index no longer valid */ }","preventionTips":["Always bounds-check against Count before GetItemAt.","Refresh Count after collection changes.","Clamp scroll/target indices to Count - 1.","Rebuild IndexedEnumerable when the source changes size."],"tags":["wpf","argument-out-of-range","indexed-enumerable","index"],"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-21T21:30:21.729Z"}