{"record":{"id":"2878358f3889744a","repo":"dotnet/reactive","slug":"thrown-when-source-is-null-argumentnullexception-param-name-287835","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/Max.cs","lineNumber":23,"sourceCode":"using System.Collections.Generic;\n\nnamespace System.Linq\n{\n    public static partial class EnumerableEx\n    {\n\n#if !(REFERENCE_ASSEMBLY && NET6_0_OR_GREATER)\n        /// <summary>\n        /// Returns the maximum value in the enumerable sequence by using the specified comparer to compare values.\n        /// </summary>\n        /// <typeparam name=\"TSource\">Source sequence element type.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"comparer\">Comparer used to determine the maximum value.</param>\n        /// <returns>Maximum value in the sequence.</returns>\n        public static TSource Max<TSource>(this IEnumerable<TSource> source, IComparer<TSource> comparer)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (comparer == null)\n                throw new ArgumentNullException(nameof(comparer));\n\n            return MaxByWithTies(source, x => x, comparer).First();\n        }\n#endif\n    }\n}\n","sourceCodeStart":5,"sourceCodeEnd":32,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/Max.cs#L5-L32","documentation":"The comparer-based Max operator finds the maximum element using a caller-supplied IComparer<TSource>. It validates arguments eagerly: a null source throws ArgumentNullException with paramName 'source' before any comparison work begins.","triggerScenarios":"Calling source.Max(comparer) (IEnumerable<TSource> + IComparer<TSource> overload) with source == null, e.g. values.Max(customComparer) where values is an uninitialized or null-returning collection.","commonSituations":"Passing results of nullable-returning parsers directly into Max; comparing sequences built conditionally where one branch leaves the variable null; the comparer overload (behind #if for newer TFMs) invoked instead of the parameterless Max.","solutions":["Ensure the source collection is initialized before calling Max; check for null and handle the empty/absent case explicitly.","Coalesce: (values ?? throw new InvalidOperationException(\"no values\")).Max(comparer).","Restructure the producer to return an empty sequence; note Max on an empty source will then throw InvalidOperationException (no elements), so handle emptiness separately."],"exampleFix":"// before\nvar max = readings.Max(tempComparer); // readings null on failed read\n// after\nvar max = readings is null ? throw new InvalidOperationException(\"no readings\") : readings.Max(tempComparer);","handlingStrategy":"validation","validationCode":"if (source is null) throw new InvalidOperationException(\"no data to take Max of\");\nif (comparer is null) comparer = Comparer<TSource>.Default;\nvar max = source.Max(comparer);","typeGuard":"static bool CanComputeMax<T>(IEnumerable<T>? source, IComparer<T>? comparer) => source is not null && comparer is not null;","tryCatchPattern":"try\n{\n    var max = source.Max(comparer);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName is \"source\" or \"comparer\")\n{\n    // handle missing source or comparer\n}","preventionTips":["Null-check both source and comparer before aggregate calls","Default comparer to Comparer<T>.Default when custom ordering is optional","Remember Max also throws InvalidOperationException on an empty source; handle that case separately"],"tags":["argumentnull","linq","dotnet","ix-net"],"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"}