{"record":{"id":"3db753e5d7703d9c","repo":"dotnet/wpf","slug":"sr-namescopenamenotemptystring-namescopedictionary","errorCode":null,"errorMessage":"SR.NameScopeNameNotEmptyString","messagePattern":"SR\\.NameScopeNameNotEmptyString","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScopeDictionary.cs","lineNumber":43,"sourceCode":"\n        public NameScopeDictionary()\n        {\n        }\n\n        public NameScopeDictionary(INameScope underlyingNameScope)\n        {\n            _names = new FrugalObjectList<string>();\n            _underlyingNameScope = underlyingNameScope ?? throw new ArgumentNullException(nameof(underlyingNameScope));\n        }\n\n        public void RegisterName(string name, object scopedElement)\n        {\n            ArgumentNullException.ThrowIfNull(name);\n\n            ArgumentNullException.ThrowIfNull(scopedElement);\n\n            if (name.Length == 0)\n                throw new ArgumentException(SR.NameScopeNameNotEmptyString);\n\n            if (!NameValidationHelper.IsValidIdentifierName(name))\n            {\n                throw new ArgumentException(SR.Format(SR.NameScopeInvalidIdentifierName, name));\n            }\n\n            if (_underlyingNameScope is not null)\n            {\n                _names.Add(name);\n                _underlyingNameScope.RegisterName(name, scopedElement);\n            }\n            else\n            {\n                if (_nameMap is null)\n                {\n                    _nameMap = new HybridDictionary();\n                    _nameMap[name] = scopedElement;\n                }","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScopeDictionary.cs#L25-L61","documentation":"NameScopeDictionary.RegisterName rejects an empty name string with ArgumentException (SR.NameScopeNameNotEmptyString). XAML names must be non-empty identifiers, so empty strings are invalid arguments for registration.","triggerScenarios":"Calling RegisterName(\"\", element) — or FindName/UnregisterName variants which apply the same check — with a zero-length name.","commonSituations":"x:Name attributes bound to data that evaluated to empty string; programmatically generated names where a format produced \"\"; parsing user input without trimming/validation.","solutions":["Validate that the name is non-empty before calling RegisterName","Trim and check the source of the name (attribute value, data binding) before use","Generate a fallback name when the computed name is empty"],"exampleFix":"// before\nnameScopeDictionary.RegisterName(name, element); // name may be \"\"\n// after\nif (!string.IsNullOrEmpty(name)) nameScopeDictionary.RegisterName(name, element);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(name)) throw new ArgumentException(\"Name must be non-empty\");\nnameScopeDictionary.RegisterName(name, element);","typeGuard":"static bool IsValidName(string name) => !string.IsNullOrEmpty(name);","tryCatchPattern":"try { nameScopeDictionary.RegisterName(name, element); }\ncatch (ArgumentException) { /* empty name */ }","preventionTips":["Trim and validate names derived from attributes or bindings","Generate fallback names instead of registering empty strings","Centralize name validation in a helper"],"tags":["wpf","system-xaml","namescope","validation"],"backgroundTag":"empty-required-field","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"}