dotnet/wpf · error · ArgumentOutOfRangeException
throw new ArgumentOutOfRangeException(nameof(index));
Error message
throw new ArgumentOutOfRangeException(nameof(index));
What it means
SingleItemList.GetKeyValuePair(int index, ref int key, ref object value) is FrugalMap's one-entry store; it only serves index 0 when the lone entry exists. Any other index — including index > 0 on a map that holds exactly one entry — throws ArgumentOutOfRangeException(nameof(index)). The map's indexer/API contract is that ordinal access must stay below Count.
Solutions
- Bound enumeration with the map's Count, not a fixed size
- Use map.Iterate(list, callback) (or GetSortedArray) instead of manual index-based access
- Check that index == 0 before calling GetKeyValuePair on a single-entry map
- For general access, use the map's Search(key)/indexer API rather than ordinal positions
Example fix
// before for (int i = 0; i < map.Count + 1; i++) map.GetKeyValuePair(i, ref key, ref value); // throws at last pass // after for (int i = 0; i < map.Count; i++) map.GetKeyValuePair(i, ref key, ref value);
Defensive patterns
Strategy: validation
Validate before calling
if (index >= 0 && index < map.Count)
map.GetKeyValuePair(index, ref key, ref value); Type guard
static bool TryGetPair(FrugalMap map, int index, ref int key, ref object value)
=> index >= 0 && index < map.Count; // single-entry maps only serve index 0 Try / catch
try { map.GetKeyValuePair(index, ref key, ref value); }
catch (ArgumentOutOfRangeException)
{
// ran past the end; stop enumeration
} Prevention
- Enumerate with i < map.Count, never Capacity
- Prefer map.Iterate(callback) or GetSortedArray over manual ordinal loops
- For single-entry maps, only request index 0
- Use Search(key)/indexer for key-based access instead of ordinals
When it happens
Trigger: Calling GetKeyValuePair with index != 0 on a FrugalMap whose store is a SingleItemList (i.e. the map contains exactly one entry with a valid key); iterating with a hand-rolled loop that doesn't stop at Count == 1.
Common situations: Custom code enumerating a FrugalMap (or the DependencyProperty/DependencyObject value store it backs) with a for-loop bounded by Capacity instead of Count, or continuing iteration after the single entry.
Related errors
- args
- args
- ArgumentOutOfRangeException(authenticationType)
- ArgumentOutOfRangeException
- ArgumentOutOfRangeException(nameof(characterHit))
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/667056abe2f25700.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/Shared/MS/Utility/FrugalMap.cs:169
}
public override void Sort()
{
// Single items are already sorted.
}
public override void GetKeyValuePair(int index, out int key, out Object value)
{
if (0 == index)
{
value = _loneEntry.Value;
key = _loneEntry.Key;
}
else
{
value = DependencyProperty.UnsetValue;
key = INVALIDKEY;
throw new ArgumentOutOfRangeException(nameof(index));
}
}
public override void Iterate(ArrayList list, FrugalMapIterationCallback callback)
{
if (Count == 1)
{
callback(list, _loneEntry.Key, _loneEntry.Value);
}
}
public override void Promote(FrugalMapBase newMap)
{
if (FrugalMapStoreState.Success == newMap.InsertEntry(_loneEntry.Key, _loneEntry.Value))
{
}
else
{View on GitHub (pinned to 81131a70a4)