{"record":{"id":"22594b680ddf65d4","repo":"dotnet/wpf","slug":"sr-namescopenamenotfound-namescopedictionary","errorCode":null,"errorMessage":"SR.NameScopeNameNotFound","messagePattern":"SR\\.NameScopeNameNotFound","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScopeDictionary.cs","lineNumber":98,"sourceCode":"            ArgumentNullException.ThrowIfNull(name);\n\n            if (name.Length == 0)\n                throw new ArgumentException(SR.NameScopeNameNotEmptyString);\n\n            if (_underlyingNameScope is not null)\n            {\n                _underlyingNameScope.UnregisterName(name);\n                _names.Remove(name);\n            }\n            else\n            {\n                if (_nameMap is not null && _nameMap[name] is not null)\n                {\n                    _nameMap.Remove(name);\n                }\n                else\n                {\n                    throw new ArgumentException(SR.Format(SR.NameScopeNameNotFound, name));\n                }\n            }\n        }\n\n        public object FindName(string name)\n        {\n            ArgumentNullException.ThrowIfNull(name);\n\n            if (name.Length == 0)\n                throw new ArgumentException(SR.NameScopeNameNotEmptyString);\n\n            if (_underlyingNameScope is not null)\n            {\n                return _underlyingNameScope.FindName(name);\n            }\n            else\n            {\n                if (_nameMap is null)","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/NameScopeDictionary.cs#L80-L116","documentation":"UnregisterName throws ArgumentException (SR.NameScopeNameNotFound, including the name) when the name is not present in the name map — the fallback branch when _nameMap is null or the name has no entry. Unlike FindName, which returns null for missing names, unregistration of a nonexistent name is an error.","triggerScenarios":"Calling UnregisterName for a name that was never registered, or that was already unregistered; double-unregister in cleanup paths; unregistering before the scope was populated.","commonSituations":"Idempotent cleanup code (e.g. on unload) calling UnregisterName twice; typos in name strings; scopes rebuilt between register and unregister calls.","solutions":["Verify the name exists (FindName != null) before unregistering","Track registrations yourself and only unregister known names","Make cleanup idempotent by catching/ignoring the not-found case or using a flag"],"exampleFix":"// before\nnameScopeDictionary.UnregisterName(\"Button1\"); // may not exist\n// after\nif (nameScopeDictionary.FindName(\"Button1\") != null) nameScopeDictionary.UnregisterName(\"Button1\");","handlingStrategy":"validation","validationCode":"if (nameScopeDictionary.FindName(name) is not null) nameScopeDictionary.UnregisterName(name);","typeGuard":"static bool IsRegistered(NameScopeDictionary d, string name) => d.FindName(name) is not null;","tryCatchPattern":"try { nameScopeDictionary.UnregisterName(name); }\ncatch (ArgumentException) { /* name never registered */ }","preventionTips":["Only unregister names you registered (keep a registry)","Make cleanup idempotent","Check FindName before UnregisterName"],"tags":["wpf","system-xaml","namescope","not-found","argumentexception"],"backgroundTag":"record-not-found","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"}