{"record":{"id":"de0e96e087418055","repo":"dotnet/reactive","slug":"thrown-when-keyselector-is-null-argumentnullexception-param-de0e96","errorCode":null,"errorMessage":"Thrown when keySelector is null (ArgumentNullException, param name: keySelector)","messagePattern":"Thrown when keySelector is null \\(ArgumentNullException, param name: keySelector\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/MaxByWithTies.cs","lineNumber":24,"sourceCode":"\nnamespace System.Linq\n{\n    public static partial class EnumerableEx\n    {\n        /// <summary>\n        /// Returns the elements with the maximum key value by using the default comparer to compare key values.\n        /// </summary>\n        /// <typeparam name=\"TSource\">Source sequence element type.</typeparam>\n        /// <typeparam name=\"TKey\">Key type.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"keySelector\">Key selector used to extract the key for each element in the sequence.</param>\n        /// <returns>List with the elements that share the same maximum key value.</returns>\n        public static IList<TSource> MaxByWithTies<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (keySelector == null)\n                throw new ArgumentNullException(nameof(keySelector));\n\n            return MaxByWithTies(source, keySelector, Comparer<TKey>.Default);\n        }\n\n        /// <summary>\n        /// Returns the elements with the minimum key value by using the specified comparer to compare key values.\n        /// </summary>\n        /// <typeparam name=\"TSource\">Source sequence element type.</typeparam>\n        /// <typeparam name=\"TKey\">Key type.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"keySelector\">Key selector used to extract the key for each element in the sequence.</param>\n        /// <param name=\"comparer\">Comparer used to determine the maximum key value.</param>\n        /// <returns>List with the elements that share the same maximum key value.</returns>\n        public static IList<TSource> MaxByWithTies<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (keySelector == null)","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/MaxByWithTies.cs#L6-L42","documentation":"MaxByWithTies(source, keySelector) throws ArgumentNullException with param name 'keySelector' when the key-selection delegate is null. The delegate is required to extract a comparison key from every element, so the operator refuses to run without it.","triggerScenarios":"Passing a null Func<TSource,TKey>, e.g. Func<Person,int> key = null; people.MaxByWithTies(key); typically when the delegate is built conditionally or fetched from config/dependency injection.","commonSituations":"Conditionally constructed lambdas, delegates resolved from a DI container or settings object that returned null, refactoring that renamed a method leaving a null method-group reference.","solutions":["Ensure a non-null lambda or method group is passed as the key selector","Guard the delegate before the call: if (keySelector == null) use a default key extractor","Check where the delegate is produced (DI, config, factory) and fix the null origin"],"exampleFix":"// before\nFunc<Order, decimal> key = GetSelector() ; // may be null\nvar top = orders.MaxByWithTies(key);\n// after\nvar key = GetSelector() ?? (o => o.Total);\nvar top = orders.MaxByWithTies(key);","handlingStrategy":"validation","validationCode":"if (keySelector is null) throw new ArgumentException(\"Key selector must not be null.\", nameof(keySelector));\nvar ties = source.MaxByWithTies(keySelector);","typeGuard":"static bool IsValidSelector<T,TKey>(Func<T,TKey> f) => f is not null;","tryCatchPattern":"try { result = source.MaxByWithTies(keySelector); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"keySelector\") { /* fall back to default selector */ }","preventionTips":["Never build delegates conditionally without an else-branch default","Check DI/config-resolved delegates for null before use","Prefer method groups over variables holding lambdas where possible"],"tags":["csharp","null-argument","delegate","linq"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}