{"record":{"id":"3268a270f37493b7","repo":"dotnet/reactive","slug":"selector-observable-aggregates","errorCode":null,"errorMessage":"selector","messagePattern":"selector","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Aggregates.cs","lineNumber":461,"sourceCode":"        /// </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, int> 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 <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        {","sourceCodeStart":443,"sourceCodeEnd":479,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Aggregates.cs#L443-L479","documentation":"System.Reactive throws ArgumentNullException when the selector delegate passed to Average<TSource>(IObservable<TSource>, Func<TSource,int>) is null. Like all Rx aggregate operators, the selector is validated immediately so failures surface at the call site rather than during subscription.","triggerScenarios":"Passing a null Func<TSource,int> to Observable.Average, e.g. xs.Average((Func<MyType,int>)null) or xs.Average(condition ? selectorA : null) where the condition is false.","commonSituations":"Optional projections resolved from DI or settings; conditional expression trees where one branch is null; passing the result of a lookup that found no delegate.","solutions":["Supply a valid selector lambda, e.g. x => (int)x.Score","Default the selector before the call when it may be absent","Verify argument order in the call is (source, selector)"],"exampleFix":"// before\nvar avg = xs.Average(config.Selector); // may be null\n// after\nFunc<Item,int> selector = config.Selector ?? (x => x.Count);\nvar avg = xs.Average(selector);","handlingStrategy":"validation","validationCode":"if (source == null) throw new ArgumentNullException(nameof(source));\nif (selector == null) throw new ArgumentNullException(nameof(selector));","typeGuard":"bool HasSelector<TSource>(Func<TSource,int>? s) => s is not null;","tryCatchPattern":"try { var avg = xs.Average(sel); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"selector\") { var avg = xs.Average(x => (int)x.Default); }","preventionTips":["Validate resolved delegates before use","Avoid conditional expressions that can yield a null branch","Keep (source, selector) argument order consistent in call sites"],"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"}