{"record":{"id":"25c6a5d514854396","repo":"dotnet/wpf","slug":"argumentexception-sr-format-sr-collectioncannotcontainnulls","errorCode":null,"errorMessage":"ArgumentException(SR.Format(SR.CollectionCannotContainNulls, \"typeArguments\"))","messagePattern":"ArgumentException\\(SR\\.Format\\(SR\\.CollectionCannotContainNulls, \"typeArguments\"\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlType.cs","lineNumber":1587,"sourceCode":"            {\n                return new ReadOnlyCollection<T>(list);\n            }\n\n            return ReadOnlyCollection<T>.Empty;\n        }\n\n        private static ReadOnlyCollection<XamlType> GetTypeArguments(IList<XamlType> typeArguments)\n        {\n            if (typeArguments is null || typeArguments.Count == 0)\n            {\n                return null;\n            }\n\n            foreach (XamlType typeArg in typeArguments)\n            {\n                if (typeArg is null)\n                {\n                    throw new ArgumentException(SR.Format(SR.CollectionCannotContainNulls, \"typeArguments\"));\n                }\n            }\n\n            return new List<XamlType>(typeArguments).AsReadOnly();\n        }\n\n        private static ReadOnlyCollection<XamlType> GetTypeArguments(Type type, XamlSchemaContext schemaContext)\n        {\n            Type genericType = type;\n            while (genericType.IsArray)\n            {\n                genericType = genericType.GetElementType();\n            }\n\n            if (!genericType.IsGenericType)\n            {\n                return null;\n            }","sourceCodeStart":1569,"sourceCodeEnd":1605,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlType.cs#L1569-L1605","documentation":"When a generic XamlType is constructed, its typeArguments list is validated: null entries are not permitted because each argument must itself be a XamlType. A null element throws ArgumentException with message CollectionCannotContainNulls('typeArguments').","triggerScenarios":"Calling a XamlType constructor (e.g. the typeName/typeArguments form) or LookupTypeArguments path with an IList<XamlType> that contains a null element, such as new XamlType[] { someType, null }.","commonSituations":"Building generic type references dynamically where an argument failed to resolve to a XamlType and null was passed through; arrays preallocated to a size but not fully populated.","solutions":["Filter or throw on null entries before passing typeArguments","Resolve every generic argument to a valid XamlType via SchemaContext.GetXamlType first","Use a correctly sized list so no trailing null slots exist"],"exampleFix":"// before\nvar args = new XamlType[2];\nargs[0] = ctx.GetXamlType(typeof(int)); // args[1] stays null\nvar t = new XamlType(\"FuncOfInt\", args, ctx); // throws\n// after\nvar args = new List<XamlType> { ctx.GetXamlType(typeof(int)), ctx.GetXamlType(typeof(string)) };\nvar t = new XamlType(\"FuncOfInt\", args, ctx);","handlingStrategy":"validation","validationCode":"if (typeArguments is not null && typeArguments.Any(a => a is null)) throw new ArgumentException(\"typeArguments cannot contain null\", nameof(typeArguments));","typeGuard":"bool allNonNull = typeArguments is null || typeArguments.All(a => a is not null);","tryCatchPattern":"try { var t = new XamlType(name, typeArguments, ctx); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"typeArguments\")) { /* filter nulls and retry */ }","preventionTips":["Resolve every generic argument with SchemaContext.GetXamlType before building the list","Avoid preallocated arrays with unfilled slots","Sanitize lists from external metadata for nulls"],"tags":["argumentexception","generics","typearguments","xaml"],"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"}