{"record":{"id":"92773a9495696ae7","repo":"dotnet/wpf","slug":"sr-textelementcollection-noenumerator","errorCode":null,"errorMessage":"SR.TextElementCollection_NoEnumerator","messagePattern":"SR\\.TextElementCollection_NoEnumerator","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs","lineNumber":321,"sourceCode":"            {\n                this.TextContainer.EndChange();\n            }\n        }\n\n        /// <summary>\n        /// Adds the elements of an IEnumerable to this collection.\n        /// </summary>\n        /// <param name=\"range\">\n        /// Elements to add.\n        /// </param>\n        public void AddRange(IEnumerable range)\n        {\n            ArgumentNullException.ThrowIfNull(range);\n\n            IEnumerator enumerator = range.GetEnumerator();\n            if (enumerator == null)\n            {\n                throw new ArgumentException(SR.TextElementCollection_NoEnumerator, nameof(range));\n            }\n\n            this.TextContainer.BeginChange();\n            try\n            {\n                while (enumerator.MoveNext())\n                {\n                    TextElementType element = enumerator.Current as TextElementType;\n\n                    if (element == null)\n                    {\n                        // REVIEW: It would be better design if we reviewed all elements in the range before starting an insert.\n                        // Otherwise, we might insert half the elements and then throw.\n                        throw new ArgumentException(SR.Format(SR.TextElementCollection_ItemHasUnexpectedType, \"range\", typeof(TextElementType).Name, typeof(TextElementType).Name), \"value\");\n                    }\n\n                    Add(element);\n                }","sourceCodeStart":303,"sourceCodeEnd":339,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs#L303-L339","documentation":"Thrown by TextElementCollection<T>.AddRange when GetEnumerator() on the supplied range returns null. AddRange requires an enumerable object that actually yields an IEnumerator to iterate and insert items.","triggerScenarios":"Calling AddRange(range) where range's GetEnumerator() returns null — typically a custom collection with a broken/returning-null GetEnumerator implementation, not a standard collection.","commonSituations":"Passing a hand-rolled enumerable whose GetEnumerator returns null; passing an object that only nominally implements IEnumerable.","solutions":["Pass a standard collection implementing IEnumerable correctly (List<T>, array, etc.).","Fix the custom type's GetEnumerator to return a valid non-null IEnumerator.","If the source may be null-yielding, enumerate it into a List first and pass that to AddRange."],"exampleFix":"// before\ncollection.AddRange(customEnumerable); // GetEnumerator() returns null\n// after\ncollection.AddRange(customEnumerable.Cast<object>().ToList());","handlingStrategy":"validation","validationCode":"var snapshot = range?.Cast<object>().ToList();\nif (snapshot != null) collection.AddRange(snapshot);","typeGuard":"bool HasValidEnumerator(System.Collections.IEnumerable e) => e?.GetEnumerator() != null;","tryCatchPattern":"try { collection.AddRange(range); }\ncatch (ArgumentException ex) when (ex.ParamName == \"range\") { /* materialize range into a List<object> and retry */ }","preventionTips":["Pass standard collections (arrays, List<T>) to AddRange.","Avoid custom IEnumerables with hand-written GetEnumerator implementations.","Materialize lazily-built or custom enumerables into a List first."],"tags":["wpf","ienumerable","invalid-argument"],"backgroundTag":"invalid-argument-value","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"}