dotnet/wpf · error · ArgumentOutOfRangeException

ArgumentOutOfRangeException(index)

Error message

ArgumentOutOfRangeException(index)

What it means

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.

Solutions

  1. Check collection.Count > 0 before indexing.
  2. Enumerate with foreach or Cast<InputBinding>() instead of by index.
  3. If the caller owns the collection, ensure at least one InputBinding was added before reads.

Example fix

// before
var first = inputBindings[0];
// after
var first = inputBindings.Count > 0 ? inputBindings[0] : null;
Defensive patterns

Strategy: validation

Validate before calling

if (inputBindings != null && index >= 0 && index < inputBindings.Count)
{ var b = inputBindings[index]; }

Type guard

static InputBinding TryGet(InputBindingCollection c, int index) =>
    (c != null && index >= 0 && index < c.Count) ? c[index] : null;

Try / catch

try { var b = inputBindings[index]; }
catch (ArgumentOutOfRangeException) { /* collection is empty */ }

Prevention

When it happens

Trigger: Reading collection[index] when no items were ever added (the lazy _innerBindingList is null), regardless of whether index is 0 or otherwise.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/d3cdbc48d79a888f. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/InputBindingCollection.cs:164

                this[index] = inputBinding;
            }
        }

#endregion Implementation of IList
        /// <summary>
        /// Indexing operator
        /// </summary>
        public InputBinding this[int index]
        {
            get
            {
                if (_innerBindingList != null)
                {
                    return _innerBindingList[index];
                }
                else
                {
                    throw new ArgumentOutOfRangeException(nameof(index));
                }
            }
            set
            {
                if (_innerBindingList != null)
                {
                    InputBinding oldInputBinding = null;
                    if (index >= 0 && index < _innerBindingList.Count)
                    {
                        oldInputBinding = _innerBindingList[index];
                    }
                    _innerBindingList[index] = value;
                    if (oldInputBinding != null)
                    {
                        InheritanceContextHelper.RemoveContextFromObject(_owner, oldInputBinding);
                    }
                    InheritanceContextHelper.ProvideContextForObject(_owner, value);
                }

View on GitHub (pinned to 81131a70a4)