{"record":{"id":"667056abe2f25700","repo":"dotnet/wpf","slug":"throw-new-argumentoutofrangeexception-nameof-index-frugalmap","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/FrugalMap.cs","lineNumber":169,"sourceCode":"        }\n\n        public override void Sort()\n        {\n            // Single items are already sorted.\n        }\n\n        public override void GetKeyValuePair(int index, out int key, out Object value)\n        {\n            if (0 == index)\n            {\n                value = _loneEntry.Value;\n                key = _loneEntry.Key;\n            }\n            else\n            {\n                value = DependencyProperty.UnsetValue;\n                key = INVALIDKEY;\n                throw new ArgumentOutOfRangeException(nameof(index));\n            }\n        }\n        \n        public override void Iterate(ArrayList list, FrugalMapIterationCallback callback)\n        {\n            if (Count == 1)\n            {\n                callback(list, _loneEntry.Key, _loneEntry.Value);\n            }\n        }\n\n        public override void Promote(FrugalMapBase newMap)\n        {\n            if (FrugalMapStoreState.Success == newMap.InsertEntry(_loneEntry.Key, _loneEntry.Value))\n            {\n            }\n            else\n            {","sourceCodeStart":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Utility/FrugalMap.cs#L151-L187","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nfor (int i = 0; i < map.Count + 1; i++) map.GetKeyValuePair(i, ref key, ref value); // throws at last pass\n// after\nfor (int i = 0; i < map.Count; i++) map.GetKeyValuePair(i, ref key, ref value);","handlingStrategy":"validation","validationCode":"if (index >= 0 && index < map.Count)\n    map.GetKeyValuePair(index, ref key, ref value);","typeGuard":"static bool TryGetPair(FrugalMap map, int index, ref int key, ref object value)\n    => index >= 0 && index < map.Count; // single-entry maps only serve index 0","tryCatchPattern":"try { map.GetKeyValuePair(index, ref key, ref value); }\ncatch (ArgumentOutOfRangeException)\n{\n    // ran past the end; stop enumeration\n}","preventionTips":["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"],"tags":["argument-out-of-range","wpf","frugal-map","enumeration"],"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"}