{"record":{"id":"12eec7d566573dff","repo":"dotnet/reactive","slug":"thrown-when-source-is-null-argumentnullexception-param-name-12eec7","errorCode":null,"errorMessage":"Thrown when source is null (ArgumentNullException, param name: source)","messagePattern":"Thrown when source is null \\(ArgumentNullException, param name: source\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/MaxBy.cs","lineNumber":24,"sourceCode":"\nnamespace System.Linq\n{\n    public static partial class EnumerableEx\n    {\n#if !(REFERENCE_ASSEMBLY && NET6_0_OR_GREATER)\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        [Obsolete(\"Use MaxByWithTies to maintain same behavior with .NET 6 and later\", false)]\n        public static IList<TSource> MaxBy<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 MaxBy(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        [Obsolete(\"Use MaxByWithTies to maintain same behavior with .NET 6 and later\", false)]\n        public static IList<TSource> MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)\n        {","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/MaxBy.cs#L6-L42","documentation":"The obsolete single-overload MaxBy selects elements with the maximum key via a keySelector function. It validates arguments eagerly: a null source throws ArgumentNullException with paramName 'source'. This overload is marked [Obsolete]; MaxByWithTies is the recommended replacement.","triggerScenarios":"Calling source.MaxBy(keySelector) where source is null, e.g. rows.MaxBy(r => r.Score) with rows == null. Compiles with an Obsolete warning; the null check throws before enumeration.","commonSituations":"Using the legacy MaxBy from older Ix-based code after upgrading, with a collection variable that can be null; copy-pasted query pipelines where an upstream operator returned null.","solutions":["Migrate to MaxByWithTies(source, keySelector), which is the supported API, and null-check the source first.","Guard the call: if (rows == null) return default list; before invoking MaxBy.","Coalesce the source with an empty collection, then handle the empty-result case (First will throw on empty)."],"exampleFix":"// before\nvar best = rows.MaxBy(r => r.Score); // rows may be null; also obsolete\n// after\nvar best = rows is null ? [] : rows.MaxByWithTies(r => r.Score);","handlingStrategy":"validation","validationCode":"if (source is null) return new List<TSource>();\nvar best = source.MaxBy(x => x.Key); // prefer MaxByWithTies","typeGuard":"static bool CanMaxBy<T, TKey>(IEnumerable<T>? source, Func<T, TKey>? keySelector) => source is not null && keySelector is not null;","tryCatchPattern":"try\n{\n    var best = source.MaxBy(x => x.Score);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    var best = new List<TSource>();\n}","preventionTips":["Migrate from the obsolete MaxBy to MaxByWithTies","Null-check source before extrema operations; decide empty/null result semantics","Enable nullable reference types to catch null collections early"],"tags":["argumentnull","linq","dotnet","ix-net","obsolete-api"],"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"}