{"record":{"id":"661c29ceb0ef548f","repo":"dotnet/maui","slug":"void-icollection-element-clear-throw-new-not","errorCode":null,"errorMessage":"void ICollection<Element>.Clear() => throw new NotSupportedException();","messagePattern":"void ICollection<Element>\\.Clear\\(\\) => throw new NotSupportedException\\(\\);","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"warning","filePath":"src/Controls/src/Core/Element/Element.cs","lineNumber":1203,"sourceCode":"\n\t\tclass TemporaryWrapper : IList<Element>\n\t\t{\n\t\t\tIReadOnlyList<Element> _inner;\n\n\t\t\tpublic TemporaryWrapper(IReadOnlyList<Element> inner)\n\t\t\t{\n\t\t\t\t_inner = inner;\n\t\t\t}\n\n\t\t\tElement IList<Element>.this[int index] { get => _inner[index]; set => throw new NotSupportedException(); }\n\n\t\t\tint ICollection<Element>.Count => _inner.Count;\n\n\t\t\tbool ICollection<Element>.IsReadOnly => true;\n\n\t\t\tvoid ICollection<Element>.Add(Element item) => throw new NotSupportedException();\n\n\t\t\tvoid ICollection<Element>.Clear() => throw new NotSupportedException();\n\n\t\t\tbool ICollection<Element>.Contains(Element item) => _inner.IndexOf(item) != -1;\n\n\t\t\tvoid ICollection<Element>.CopyTo(Element[] array, int arrayIndex) => throw new NotSupportedException();\n\n\t\t\tIEnumerator<Element> IEnumerable<Element>.GetEnumerator() => _inner.GetEnumerator();\n\n\t\t\tSystem.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => _inner.GetEnumerator();\n\n\t\t\tint IList<Element>.IndexOf(Element item) => _inner.IndexOf(item);\n\n\t\t\tvoid IList<Element>.Insert(int index, Element item) => throw new NotSupportedException();\n\n\t\t\tbool ICollection<Element>.Remove(Element item) => throw new NotSupportedException();\n\n\t\t\tvoid IList<Element>.RemoveAt(int index) => throw new NotSupportedException();\n\t\t}\n\t}","sourceCodeStart":1185,"sourceCodeEnd":1221,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/Element/Element.cs#L1185-L1221","documentation":"Thrown by TemporaryWrapper.Clear(). TemporaryWrapper is the read-only IList<Element> adapter behind the obsolete Element.LogicalChildren ReadOnlyCollection. Because IsReadOnly is true and the inner IReadOnlyList cannot be cleared, Clear throws NotSupportedException.","triggerScenarios":"Calling Clear on the internal TemporaryWrapper obtained via reflection or internal access. The public ReadOnlyCollection wrapper would block this first; reaching TemporaryWrapper.Clear requires accessing the adapter directly.","commonSituations":"Hot Reload or designer tooling attempting to reset logical children by clearing the backing collection; reflection-based code treating the wrapper as mutable.","solutions":["Remove logical children individually via RemoveLogicalChild rather than clearing the collection.","Treat LogicalChildren and its backing list as read-only.","Avoid writing to internal Element collections."],"exampleFix":"// before (reflection on internal wrapper)\nwrapper.Clear(); // NotSupportedException\n// after\nforeach (var child in element.LogicalChildren.ToList())\n    element.RemoveLogicalChild(child);","handlingStrategy":"validation","validationCode":"if (element.LogicalChildren.IsReadOnly)\n    return; // do not Clear the backing wrapper","typeGuard":null,"tryCatchPattern":"// Adapter reached only via reflection; remove children individually via RemoveLogicalChild.","preventionTips":["Remove children via RemoveLogicalChild in a loop instead of clearing the collection.","Treat LogicalChildren as read-only.","Do not manipulate internal collections via reflection."],"tags":["maui","readonly-collection","logicalchildren","notsupported"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}