{"record":{"id":"7f9b736348306a36","repo":"dotnet/maui","slug":"throw-new-argumentoutofrangeexception","errorCode":null,"errorMessage":"throw new ArgumentOutOfRangeException();","messagePattern":"throw new ArgumentOutOfRangeException\\(\\);","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/Element/Element.cs","lineNumber":959,"sourceCode":"\t\t\t\t\tbreak;\n\t\t\t\tcase NotifyCollectionChangedAction.Reset:\n\t\t\t\t\tif (e.NewItems != null)\n\t\t\t\t\t{\n\t\t\t\t\t\tforeach (Effect effect in e.NewItems)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tAttachEffect(effect);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (e.OldItems != null)\n\t\t\t\t\t{\n\t\t\t\t\t\tforeach (Effect effect in e.OldItems)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\teffect.ClearEffect();\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tbreak;\n\t\t\t\tdefault:\n\t\t\t\t\tthrow new ArgumentOutOfRangeException();\n\t\t\t}\n\t\t}\n\n\t\tinternal INameScope GetNameScope()\n\t\t{\n\t\t\tif (!RuntimeFeature.AreNamescopesSupported)\n\t\t\t\tthrow new NotSupportedException(\"Namescopes are not supported. Please enable the feature switch 'Microsoft.Maui.RuntimeFeature.AreNamescopesSupported' to keep using namescopes.\");\n\n\t\t\tvar element = this;\n\t\t\tdo\n\t\t\t{\n\t\t\t\tvar ns = NameScope.GetNameScope(element);\n\t\t\t\tif (ns != null)\n\t\t\t\t\treturn ns;\n\t\t\t} while ((element = element.RealParent) != null);\n\t\t\treturn null;\n\t\t}\n","sourceCodeStart":941,"sourceCodeEnd":977,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/Element/Element.cs#L941-L977","documentation":"Thrown as the default case of the switch over NotifyCollectionChangedEventArgs.Action in EffectsOnCollectionChanged. The switch handles Add, Move, Remove, Replace, Reset explicitly; any other enum value (which should not exist in the standard NotifyCollectionChangedAction enum) hits the default and throws ArgumentOutOfRangeException with no message. This is a defensive invariant guard.","triggerScenarios":"A custom collection or external code raises a CollectionChanged event on the Element.Effects collection with an Action value outside the known enum, or casts an arbitrary integer to NotifyCollectionChangedAction. Extremely unlikely under normal MAUI usage; indicates a corrupted or non-standard collection event.","commonSituations":"Third-party collection wrappers forwarding malformed change notifications; reflection-based manipulation of the Effects collection; a bug in a custom ObservableCollection subclass raising events with invalid Action values.","solutions":["Avoid raising CollectionChanged on the Effects collection directly — use the Effects.Add/Remove API.","If wrapping the collection, ensure NotifyCollectionChangedEventArgs.Action is always one of Add/Move/Remove/Replace/Reset.","Inspect the source raising the event; this error indicates a logic bug upstream, not a configuration issue."],"exampleFix":"// before — raising with an invalid action\neffectsCollection.CollectionChanged?.Invoke(this,\n    new NotifyCollectionChangedEventArgs((NotifyCollectionChangedAction)999));\n// after — use the public API\nelement.Effects.Add(myEffect);","handlingStrategy":"try-catch","validationCode":"// Not applicable at the caller boundary; this guards an internal invariant.\n// Validate upstream: only raise CollectionChanged with known NotifyCollectionChangedAction values.","typeGuard":"static bool IsValidAction(NotifyCollectionChangedAction a) =>\n    a == NotifyCollectionChangedAction.Add ||\n    a == NotifyCollectionChangedAction.Move ||\n    a == NotifyCollectionChangedAction.Remove ||\n    a == NotifyCollectionChangedAction.Replace ||\n    a == NotifyCollectionChangedAction.Reset;","tryCatchPattern":"// Internal framework code; callers should not raise events on the Effects collection.\n// If wrapping, validate Action before raising.","preventionTips":["Use the Effects.Add/Remove API instead of raising CollectionChanged manually.","Validate NotifyCollectionChangedEventArgs.Action before forwarding events.","Avoid casting arbitrary integers to NotifyCollectionChangedAction."],"tags":["maui","effects","collectionchanged","argumentoutofrange","defensive"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}