{"record":{"id":"eec860a8222d80bd","repo":"PrismLibrary/Prism","slug":"argumentnullexception-nameof-key","errorCode":null,"errorMessage":"ArgumentNullException(nameof(key))","messagePattern":"ArgumentNullException\\(nameof\\(key\\)\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Navigation/Regions/RegionBehaviorCollection.cs","lineNumber":43,"sourceCode":"        /// Gets the <see cref=\"IRegionBehavior\"/> with the specified key.\n        /// </summary>\n        /// <value>The RegionBehavior that's registered with the key.</value>\n        public IRegionBehavior this[string key] => behaviors[key];\n\n        /// <summary>\n        /// Adds a <see cref=\"IRegionBehavior\"/> to the collection, using the specified key as an indexer.\n        /// </summary>\n        /// <param name=\"key\">The key that specifies the type of <see cref=\"IRegionBehavior\"/> that's added.</param>\n        /// <param name=\"regionBehavior\">The <see cref=\"IRegionBehavior\"/> to add.</param>\n        /// <exception cref=\"ArgumentNullException\">\n        /// Thrown is the <paramref name=\"key\"/> parameter is Null,\n        /// or if the <paramref name=\"regionBehavior\"/> parameter is Null.\n        /// </exception>\n        /// <exception cref=\"ArgumentException\">Thrown if a behavior with the specified Key parameter already exists.</exception>\n        public void Add(string key, IRegionBehavior regionBehavior)\n        {\n            if (key == null)\n                throw new ArgumentNullException(nameof(key));\n\n            if (regionBehavior == null)\n                throw new ArgumentNullException(nameof(regionBehavior));\n\n            if (behaviors.ContainsKey(key))\n                throw new ArgumentException(\"Could not add duplicate behavior with same key.\", nameof(key));\n\n            behaviors.Add(key, regionBehavior);\n            regionBehavior.Region = region;\n\n            regionBehavior.Attach();\n        }\n\n        /// <summary>\n        /// Checks if a <see cref=\"IRegionBehavior\"/> with the specified key is already present.\n        /// </summary>\n        /// <param name=\"key\">The key to use to find a particular <see cref=\"IRegionBehavior\"/>.</param>\n        /// <returns></returns>","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Navigation/Regions/RegionBehaviorCollection.cs#L25-L61","documentation":"RegionBehaviorCollection.Add throws ArgumentNullException when the key string is null. The key is used to look up behaviors in the internal dictionary, so it must be a non-null identifier (conventionally the behavior's name).","triggerScenarios":"Calling region.Behaviors.Add(null, behavior), often when the key comes from a constant, config value, or typeof expression that resolved to null.","commonSituations":"Copying behavior registration code where the key constant was deleted or renamed; building the key dynamically from settings that are missing.","solutions":["Pass a non-null key, typically nameof(MyRegionBehavior)","Define the key as a public const string on the behavior class","Validate the key before calling Add"],"exampleFix":"// before\nregion.Behaviors.Add(null, new MyBehavior());\n// after\nregion.Behaviors.Add(nameof(MyBehavior), new MyBehavior());","handlingStrategy":"validation","validationCode":"if (key != null && behavior != null) region.Behaviors.Add(key, behavior);","typeGuard":"bool ValidRegistration(string k, IRegionBehavior b) => !string.IsNullOrEmpty(k) && b != null;","tryCatchPattern":"try { region.Behaviors.Add(key, b); } catch (ArgumentNullException ex) when (ex.ParamName == \"key\") { /* fix key */ }","preventionTips":["Use nameof(BehaviorType) as key","Define keys as const strings","Null-check config-derived keys"],"tags":["prism","regions","null-check","collection"],"backgroundTag":"null-argument","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"}