{"record":{"id":"d3cdbc48d79a888f","repo":"dotnet/wpf","slug":"argumentoutofrangeexception-index","errorCode":null,"errorMessage":"ArgumentOutOfRangeException(index)","messagePattern":"ArgumentOutOfRangeException\\(index\\)","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/InputBindingCollection.cs","lineNumber":164,"sourceCode":"                this[index] = inputBinding;\n            }\n        }\n\n#endregion Implementation of IList\n        /// <summary>\n        /// Indexing operator\n        /// </summary>\n        public InputBinding this[int index]\n        {\n            get\n            {\n                if (_innerBindingList != null)\n                {\n                    return _innerBindingList[index];\n                }\n                else\n                {\n                    throw new ArgumentOutOfRangeException(nameof(index));\n                }\n            }\n            set\n            {\n                if (_innerBindingList != null)\n                {\n                    InputBinding oldInputBinding = null;\n                    if (index >= 0 && index < _innerBindingList.Count)\n                    {\n                        oldInputBinding = _innerBindingList[index];\n                    }\n                    _innerBindingList[index] = value;\n                    if (oldInputBinding != null)\n                    {\n                        InheritanceContextHelper.RemoveContextFromObject(_owner, oldInputBinding);\n                    }\n                    InheritanceContextHelper.ProvideContextForObject(_owner, value);\n                }","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/InputBindingCollection.cs#L146-L182","documentation":"InputBindingCollection's indexer getter throws ArgumentOutOfRangeException(nameof(index)) when the collection has never been populated (_innerBindingList is null) — i.e. reading any index from an empty collection throws rather than being reachable. The exception message names the 'index' parameter.","triggerScenarios":"Reading collection[index] when no items were ever added (the lazy _innerBindingList is null), regardless of whether index is 0 or otherwise.","commonSituations":"Iterating UIElement.InputBindings by index without checking Count; code assuming the collection is non-empty; casting to IList and calling indexer or CopyTo on a freshly created InputBindingCollection.","solutions":["Check collection.Count > 0 before indexing.","Enumerate with foreach or Cast<InputBinding>() instead of by index.","If the caller owns the collection, ensure at least one InputBinding was added before reads."],"exampleFix":"// before\nvar first = inputBindings[0];\n// after\nvar first = inputBindings.Count > 0 ? inputBindings[0] : null;","handlingStrategy":"validation","validationCode":"if (inputBindings != null && index >= 0 && index < inputBindings.Count)\n{ var b = inputBindings[index]; }","typeGuard":"static InputBinding TryGet(InputBindingCollection c, int index) =>\n    (c != null && index >= 0 && index < c.Count) ? c[index] : null;","tryCatchPattern":"try { var b = inputBindings[index]; }\ncatch (ArgumentOutOfRangeException) { /* collection is empty */ }","preventionTips":["Always check Count before indexed access.","Prefer foreach over index loops for WPF collections.","Note the collection is lazily allocated — even index 0 throws when empty."],"tags":["wpf","inputbinding","index"],"backgroundTag":"index-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"}