{"record":{"id":"b8983c5800c103f7","repo":"PrismLibrary/Prism","slug":"resources-regionmanagerwithdifferentnameexception-formatted","errorCode":null,"errorMessage":"Resources.RegionManagerWithDifferentNameException (formatted with region.Name, regionName)","messagePattern":"Resources\\.RegionManagerWithDifferentNameException \\(formatted with region\\.Name, regionName\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/Navigation/Regions/RegionCollection.cs","lineNumber":110,"sourceCode":"        Xaml.RegionManager.UpdateRegions();\n\n        return GetRegionByName(regionName) != null;\n    }\n\n    /// <summary>\n    /// Adds a region to the <see cref=\"RegionManager\"/> with the name received as argument.\n    /// </summary>\n    /// <param name=\"regionName\">The name to be given to the region.</param>\n    /// <param name=\"region\">The region to be added to the <see cref=\"RegionManager\"/>.</param>\n    /// <exception cref=\"ArgumentNullException\">Thrown if <paramref name=\"region\"/> is <see langword=\"null\"/>.</exception>\n    /// <exception cref=\"ArgumentException\">Thrown if <paramref name=\"regionName\"/> and <paramref name=\"region\"/>'s name do not match and the <paramref name=\"region\"/> <see cref=\"IRegion.Name\"/> is not <see langword=\"null\"/>.</exception>\n    public void Add(string regionName, IRegion region)\n    {\n        if (region == null)\n            throw new ArgumentNullException(nameof(region));\n\n        if (region.Name != null && region.Name != regionName)\n            throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.RegionManagerWithDifferentNameException, region.Name, regionName), nameof(regionName));\n\n        if (region.Name == null)\n            region.Name = regionName;\n\n        Add(region);\n    }\n\n    private IRegion GetRegionByName(string regionName) =>\n        _regions.FirstOrDefault(r => r.Name == regionName);\n\n    private void OnCollectionChanged(NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs) =>\n        CollectionChanged?.Invoke(this, notifyCollectionChangedEventArgs);\n}\n","sourceCodeStart":92,"sourceCodeEnd":124,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/Navigation/Regions/RegionCollection.cs#L92-L124","documentation":"RegionCollection.Add(string regionName, IRegion region) throws ArgumentException (parameter regionName) when the region already has a non-null Name that differs from the regionName argument. The two must match; otherwise the registration is ambiguous.","triggerScenarios":"Calling Regions.Add(\"A\", region) where region.Name is set and equals something other than \"A\" (e.g. region.Name == \"B\").","commonSituations":"Registering a region under an alias or renamed key while the region instance already carries its original name; mismatched string literals between region creation and registration; refactors renaming one side but not the other.","solutions":["Pass the regionName that matches region.Name exactly.","Set region.Name = null before adding under a new name (Add will then assign regionName).","Use the single-argument Add(region) overload if the region's existing name is authoritative."],"exampleFix":"// before\nregion.Name = \"ContentRegion\";\nregionManager.Regions.Add(\"MainRegion\", region); // name mismatch\n// after\nregionManager.Regions.Add(\"ContentRegion\", region);","handlingStrategy":"validation","validationCode":"if (!string.Equals(region?.Name, regionName) && region?.Name != null)\n    throw new InvalidOperationException($\"Region name '{region.Name}' does not match '{regionName}'\");\nregionManager.Regions.Add(regionName, region);","typeGuard":"bool NamesMatch(IRegion r, string name) => r?.Name is null || r.Name == name;","tryCatchPattern":"try { regionManager.Regions.Add(regionName, region); }\ncatch (ArgumentException ex) { Console.WriteLine($\"Name mismatch: {ex.Message}\"); }","preventionTips":["Define region names as constants shared between creation and registration","Pass the same name used when the region was created","Let Add assign the name: set region.Name = null first"],"tags":["prism","regions","name-mismatch","argumentexception"],"backgroundTag":"invalid-argument-value","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}