{"record":{"id":"0e07cfc087522bc0","repo":"dotnet/reactive","slug":"argumentnullexception-max-generated","errorCode":null,"errorMessage":"ArgumentNullException","messagePattern":"ArgumentNullException","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Max.Generated.cs","lineNumber":126,"sourceCode":"                source,\n                selector,\n                static (source, selector, observer) => source.SubscribeSafeAsync(AsyncObserver.MaxInt64(observer, selector)));\n        }\n\n        public static IAsyncObservable<Int64?> Max(this IAsyncObservable<Int64?> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.MaxNullableInt64(observer)));\n        }\n\n        public static IAsyncObservable<Int64?> Max<TSource>(this IAsyncObservable<TSource> source, Func<TSource, Int64?> selector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (selector == null)\n                throw new ArgumentNullException(nameof(selector));\n\n            return CreateAsyncObservable<Int64?>.From(\n                source,\n                selector,\n                static (source, selector, observer) => source.SubscribeSafeAsync(AsyncObserver.MaxNullableInt64(observer, selector)));\n        }\n\n        public static IAsyncObservable<Int64?> Max<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<Int64?>> selector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (selector == null)\n                throw new ArgumentNullException(nameof(selector));\n\n            return CreateAsyncObservable<Int64?>.From(\n                source,\n                selector,\n                static (source, selector, observer) => source.SubscribeSafeAsync(AsyncObserver.MaxNullableInt64(observer, selector)));","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Max.Generated.cs#L108-L144","documentation":"This generated Max overload (selector returning long?) validates its inputs up front: both the source sequence and the selector delegate must be non-null. Passing null for either throws ArgumentNullException before any subscription is created.","triggerScenarios":"Calling Max<TSource>(source, Func<TSource, long?> selector) with source == null or selector == null, e.g. await null.Max(x => (long?)x.Length) or a null delegate variable.","commonSituations":"Chaining Max onto a method whose result was null; passing a selector captured from an uninitialized field; null propagating from an earlier failed lookup before the aggregate call.","solutions":["Ensure the source IAsyncObservable is non-null before calling Max","Ensure the selector delegate is a non-null method group or lambda","If the source can be null in your flow, coalesce to an empty observable (e.g. AsyncObservable.Empty<TSource>())"],"exampleFix":"// before\nFunc<string, long?> sel = null;\nvar max = await source.Max(sel); // ArgumentNullException\n// after\nvar max = await source.Max((string x) => (long?)x.Length);","handlingStrategy":"validation","validationCode":"if (source is null) throw new ArgumentNullException(nameof(source));\nif (selector is null) throw new ArgumentNullException(nameof(selector));\nvar max = await source.Max(selector);","typeGuard":"static bool CanMax<TSource>(IAsyncObservable<TSource>? s, Func<TSource, long?>? sel) => s is not null && sel is not null;","tryCatchPattern":"try\n{\n    var max = await source.Max(selector);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName is \"source\" or \"selector\")\n{\n    max = null; // or log and use a default\n}","preventionTips":["Coalesce possibly-null observables to AsyncObservable.Empty before aggregating","Avoid nullable delegate fields for selectors; initialize at declaration","Use ArgumentNullException.ThrowIfNull in wrappers around library calls"],"tags":["csharp","async-rx","max-operator","argument-validation"],"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"}