{"record":{"id":"e9813aae46e256d0","repo":"dotnet/wpf","slug":"sr-format-sr-stylus-indexoutofrange-index-tostring-system","errorCode":null,"errorMessage":"SR.Format(SR.Stylus_IndexOutOfRange, index.ToString(System.Globalization.CultureInfo.InvariantCulture))","messagePattern":"SR\\.Format\\(SR\\.Stylus_IndexOutOfRange, index\\.ToString\\(System\\.Globalization\\.CultureInfo\\.InvariantCulture\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Common/TabletDeviceCollection.cs","lineNumber":73,"sourceCode":"        /// of TabletDevices.\n        /// </summary>\n        /// <param name=\"array\">destination array</param>\n        /// <param name=\"index\">position in destination array to begin copying</param>\n        public void CopyTo(TabletDevice[] array, int index)\n        {\n            TabletDevices.CopyTo(array, index);\n        }\n\n        /// <summary>\n        /// Retrieve the specified TabletDevice object from the collection.\n        /// </summary>\n        /// <param name=\"index\">index of TabletDevice in collection to retrieve</param>\n        public TabletDevice this[int index]\n        {\n            get\n            {\n                if (index >= Count || index < 0)\n                    throw new ArgumentException(SR.Format(SR.Stylus_IndexOutOfRange, index.ToString(System.Globalization.CultureInfo.InvariantCulture)), nameof(index));\n\n                return TabletDevices[index];\n            }\n        }\n\n        /// <summary>\n        /// Returns an object which can be used to lock during synchronization by collection users.\n        /// <seealso cref=\"System.Collections.ICollection.SyncRoot\"/>\n        /// </summary>\n        public object SyncRoot\n        {\n            get\n            {\n                return this;\n            }\n        }\n\n        /// <summary>","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Common/TabletDeviceCollection.cs#L55-L91","documentation":"TabletDeviceCollection's indexer throws ArgumentException (SR.Stylus_IndexOutOfRange with the index formatted invariantly) when the index is negative or >= Count. Note it throws ArgumentException rather than the more conventional ArgumentOutOfRangeException.","triggerScenarios":"Indexing TabletDeviceCollection with index < 0 or index >= collection Count, e.g. tabletDevices[n] where n came from a count computed elsewhere.","commonSituations":"Off-by-one loops over tablet devices; caching a count from before a device was unplugged (device count changed between reads); iterating with an index from stale code.","solutions":["Check index against Count before indexing: if (index >= 0 && index < tablets.Count)","Iterate with foreach instead of manual indexing","Re-fetch the collection rather than caching indices across device changes"],"exampleFix":"// before\nvar device = Tablet.TabletDevices[i]; // i may be stale\n// after\nif (i >= 0 && i < Tablet.TabletDevices.Count)\n{\n    var device = Tablet.TabletDevices[i];\n}","handlingStrategy":"try-catch","validationCode":"if (index < 0 || index >= Tablet.TabletDevices.Count) return null;","typeGuard":null,"tryCatchPattern":"try { var device = Tablet.TabletDevices[i]; }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"index\")) { /* refresh count and retry */ }","preventionTips":["Prefer foreach over manual indexing","Re-read Count immediately before indexing; device arrival/removal changes it"],"tags":["wpf","stylus","index-out-of-range"],"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"}