{"record":{"id":"f7b73b8178d850d7","repo":"dotnet/reactive","slug":"source-observable-aggregates","errorCode":null,"errorMessage":"source","messagePattern":"source","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Aggregates.cs","lineNumber":482,"sourceCode":"            return s_impl.Average(source, selector);\n        }\n\n        /// <summary>\n        /// Computes the average of an observable sequence of <see cref=\"long\" /> values that are obtained by invoking a transform function on each element of the input sequence.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">A sequence of values to calculate the average of.</param>\n        /// <param name=\"selector\">A transform function to apply to each element.</param>\n        /// <returns>An observable sequence containing a single element with the average of the sequence of values.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"selector\"/> is null.</exception>\n        /// <exception cref=\"InvalidOperationException\">(Asynchronous) The source sequence is empty.</exception>\n        /// <exception cref=\"OverflowException\">(Asynchronous) The sum of the projected values for the elements in the source sequence is larger than <see cref=\"long.MaxValue\"/>.</exception>\n        /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>\n        public static IObservable<double> Average<TSource>(this IObservable<TSource> source, Func<TSource, long> selector)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (selector == null)\n            {\n                throw new ArgumentNullException(nameof(selector));\n            }\n\n            return s_impl.Average(source, selector);\n        }\n\n        /// <summary>\n        /// Computes the average of an observable sequence of nullable <see cref=\"decimal\" /> values that are obtained by invoking a transform function on each element of the input sequence.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">A sequence of values to calculate the average of.</param>\n        /// <param name=\"selector\">A transform function to apply to each element.</param>\n        /// <returns>An observable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"selector\"/> is null.</exception>","sourceCodeStart":464,"sourceCodeEnd":500,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Aggregates.cs#L464-L500","documentation":"System.Reactive throws ArgumentNullException when the source sequence passed to Average<TSource>(IObservable<TSource>, Func<TSource,long>) is null. The operator validates the source before the selector and before returning the observable, throwing synchronously at invocation time.","triggerScenarios":"Calling xs.Average(x => (long)x.Ticks) where xs is null, typically because an upstream method returned null or a field was never assigned.","commonSituations":"Repository/service methods returning null observables on error paths; event-stream fields not yet wired; unit-test setups that forgot to initialize the subject.","solutions":["Initialize the observable before calling Average","Use Observable.Empty<long>() semantics via a null-coalescing fallback for absent data","Fix the upstream producer to return Observable.Empty instead of null"],"exampleFix":"// before\nvar avg = GetTicksStream().Average(t => (long)t);\n// after\nvar stream = GetTicksStream() ?? Observable.Empty<MyTick>();\nvar avg = stream.Average(t => (long)t.Ticks);","handlingStrategy":"validation","validationCode":"if (src is null) src = Observable.Empty<TSource>();\nif (selector == null) throw new ArgumentNullException(nameof(selector));","typeGuard":"bool HasSource<TSource>(IObservable<TSource>? s) => s is not null;","tryCatchPattern":"try { var avg = src.Average(x => (long)x.Ticks); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\") { /* fix upstream null producer */ }","preventionTips":["Treat null IObservable as a bug; use empty sequences for 'no data'","Null-check results of factory methods returning observables","Wire event-stream fields during initialization"],"tags":["csharp","rx","null-argument","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"}