{"record":{"id":"19920fe017f5b248","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-comparer-max","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'comparer')","messagePattern":"Value cannot be null\\. \\(Parameter 'comparer'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Max.cs","lineNumber":25,"sourceCode":"\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TSource> Max<TSource>(IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.Max(observer)));\n        }\n\n        public static IAsyncObservable<TSource> Max<TSource>(IAsyncObservable<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 CreateAsyncObservable<TSource>.From(\n                source,\n                comparer,\n                static (source, comparer, observer) => source.SubscribeSafeAsync(AsyncObserver.Max(observer, comparer)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> Max<TSource>(IAsyncObserver<TSource> observer)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n\n            return Max(observer, Comparer<TSource>.Default);\n        }\n","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Max.cs#L7-L43","documentation":"AsyncObservable.Max<TSource>(source, IComparer<TSource>) throws ArgumentNullException('comparer') when the comparer argument is null. Unlike the simple overload, this overload requires an explicit IComparer<TSource>; passing null defeats the comparison logic so the library fails fast instead of silently falling back.","triggerScenarios":"Calling AsyncObservable.Max(source, null) — e.g. a comparer field/property that was never assigned, a config-selected comparer that resolved to null, or passing null intending to get the default comparer.","commonSituations":"Migrating code from Comparer-based APIs where null meant 'use default'; conditional comparer selection (e.g. by culture) where the branch didn't assign; reflection or DI lookups that returned null.","solutions":["Pass Comparer<T>.Default if you want default comparison instead of null.","Assign a concrete comparer such as Comparer<int>.Create((a,b) => a.CompareTo(b)) or a custom class implementing IComparer<T>.","Coalesce at the call site: comparer ?? Comparer<T>.Default."],"exampleFix":"// before\nvar max = AsyncObservable.Max(source, comparer); // comparer is null\n\n// after\nvar max = AsyncObservable.Max(source, comparer ?? Comparer<int>.Default);","handlingStrategy":"validation","validationCode":"if (comparer is null) comparer = Comparer<TSource>.Default; // then call Max(source, comparer)","typeGuard":"static bool HasComparer<TSource>(IComparer<TSource>? c) => c is not null;","tryCatchPattern":"try { var max = AsyncObservable.Max(source, comparer); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"comparer\") { comparer = Comparer<TSource>.Default; /* retry */ }","preventionTips":["Never pass null where the API expects IComparer<T>; use Comparer<T>.Default","Centralize comparer selection in one factory that always returns non-null","Guard config-driven comparer lookups with a fallback to the default comparer"],"tags":["dotnet","null-argument","asyncrx","comparer"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}