{"record":{"id":"3827be4e034a5cc1","repo":"dotnet/wpf","slug":"sr-visual-argumentoutofrange","errorCode":null,"errorMessage":"SR.Visual_ArgumentOutOfRange","messagePattern":"SR\\.Visual_ArgumentOutOfRange","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Visual.cs","lineNumber":2514,"sourceCode":"            }\n        }\n\n        /// <summary>\n        ///   Derived class must implement to support Visual children. The method must return\n        ///    the child at the specified index. Index must be between 0 and GetVisualChildrenCount-1.\n        ///\n        ///    By default a Visual does not have any children.\n        ///\n        ///  Remark:\n        ///       Need to lock down Visual tree during the callbacks.\n        ///       During this virtual call it is not valid to modify the Visual tree.\n        ///\n        ///       It is okay to type this protected API to the 2D Visual.  The only 2D Visual with\n        ///       3D childern is the Viewport3DVisual which is sealed\n        /// </summary>\n        protected virtual Visual GetVisualChild(int index)\n        {\n           throw new ArgumentOutOfRangeException(nameof(index), index, SR.Visual_ArgumentOutOfRange);\n        }\n\n        /// <summary>\n        /// Returns the 2D child at index \"index\". This will fail for Visuals\n        /// whose children are Visual3Ds.\n        /// </summary>\n        internal Visual InternalGetVisualChild(int index)\n        {\n            // Call the right virtual method.\n            return GetVisualChild(index);\n        }\n\n        /// <summary>\n        /// Returns the child at index \"index\" (in most cases this will be\n        /// a Visual, but it some cases, Viewport3DVisual for instance,\n        /// this is a Visual3D).\n        ///\n        /// Used only by VisualTreeHelper.","sourceCodeStart":2496,"sourceCodeEnd":2532,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Visual.cs#L2496-L2532","documentation":"Visual.GetVisualChild is a protected virtual that the base Visual implementation throws ArgumentOutOfRangeException from, indicating the index is outside the visual child count. Subclasses that do not override GetVisualChildCount/GetVisualChild (or that call it with an out-of-range index) hit this.","triggerScenarios":"Calling VisualTreeHelper.GetChild on a custom Visual whose GetVisualChildCount returns an inconsistent number, or invoking GetVisualChild(index) with index < 0 or >= child count on the base Visual.","commonSituations":"Custom visuals that override only one of VisualChildrenCount/GetVisualChild; iterating children with a stale or wrong count (e.g. count cached before tree changed).","solutions":["Ensure GetVisualChild is overridden consistently with GetVisualChildCount on custom visuals","Re-read the child count immediately before each GetVisualChild call","Clamp loop indices to VisualTreeHelper.GetChildrenCount(visual) at call time"],"exampleFix":"// before\nfor (int i = 0; i < 2; i++) GetVisualChild(i);\n// after\nint n = VisualTreeHelper.GetChildrenCount(this);\nfor (int i = 0; i < n; i++) GetVisualChild(i);","handlingStrategy":"validation","validationCode":"if (index < 0 || index >= VisualTreeHelper.GetChildrenCount(visual)) return null;","typeGuard":"bool HasChildIndex(Visual v, int i) => i >= 0 && i < VisualTreeHelper.GetChildrenCount(v);","tryCatchPattern":"try { child = VisualTreeHelper.GetChild(v, i); } catch (ArgumentOutOfRangeException) { child = null; }","preventionTips":["Keep GetVisualChildCount and GetVisualChild in sync in custom visuals","Always fetch child count immediately before iterating","Never cache child counts across tree mutations"],"tags":["wpf","visual-tree","argument-out-of-range"],"backgroundTag":"argument-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}