{"record":{"id":"c9a8f79dc0d260b7","repo":"dotnet/wpf","slug":"sr-collection-nonull","errorCode":null,"errorMessage":"SR.Collection_NoNull","messagePattern":"SR\\.Collection_NoNull","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/FreezableCollection.cs","lineNumber":81,"sourceCode":"\n            ArgumentNullException.ThrowIfNull(collection);\n\n            int count = GetCount(collection);\n\n            if (count > 0)\n            {\n                _collection = new List<T>(count);\n            }\n            else\n            {\n                _collection = new List<T>();\n            }\n\n            foreach (T item in collection)\n            {\n                if (item == null)\n                {\n                    throw new System.ArgumentException(SR.Collection_NoNull);\n                }\n\n                OnFreezablePropertyChanged(oldValue: null, item);\n\n                _collection.Add(item);\n            }\n\n            WritePostscript();\n        }\n\n        #endregion Constructors\n\n        //------------------------------------------------------\n        //\n        //  Public Methods\n        //\n        //------------------------------------------------------\n","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/FreezableCollection.cs#L63-L99","documentation":"The FreezableCollection(T) constructor accepting an IEnumerable(T) throws ArgumentException (SR.Collection_NoNull) if any element of the source collection is null. Freezable collections never permit null items because null cannot be frozen/validated as a Freezable element.","triggerScenarios":"new FreezableCollection<T>(someEnumerable) where someEnumerable contains one or more null references.","commonSituations":"Initializing a collection from LINQ results, deserialized data, or an array built incrementally where nulls slipped in; e.g. new FreezableCollection<Geometry>(geometries) with geometries containing nulls.","solutions":["Filter nulls before constructing: collection.Where(x => x != null).","Fix the upstream producer so it never emits null elements.","If nulls are meaningful, wrap items in a nullable-holder type instead."],"exampleFix":"// before\nvar col = new FreezableCollection<Geometry>(rawList);\n// after\nvar col = new FreezableCollection<Geometry>(rawList.Where(g => g != null));","handlingStrategy":"validation","validationCode":"if (source.Any(item => item == null)) throw new InvalidOperationException(\"Source contains null items\");","typeGuard":null,"tryCatchPattern":"try { col = new FreezableCollection<T>(source); } catch (ArgumentException) { col = new FreezableCollection<T>(source.Where(x => x != null)); }","preventionTips":["Filter nulls from enumerable sources before constructing.","Ensure upstream producers never emit null items.","Model 'missing' values with a default instance rather than null."],"tags":["wpf","freezable","collection","null"],"backgroundTag":"null-argument","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"}