{"record":{"id":"8c85dc22d8e73172","repo":"dotnet/reactive","slug":"strings-linq-no-elements-average","errorCode":null,"errorMessage":"Strings_Linq.NO_ELEMENTS","messagePattern":"Strings_Linq\\.NO_ELEMENTS","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable/Average.cs","lineNumber":59,"sourceCode":"                }\n                catch (Exception ex)\n                {\n                    ForwardOnError(ex);\n                }\n            }\n\n            public override void OnCompleted()\n            {\n                if (_count > 0)\n                {\n                    ForwardOnNext(_sum / _count);\n                    ForwardOnCompleted();\n                }\n                else\n                {\n                    try\n                    {\n                        throw new InvalidOperationException(Strings_Linq.NO_ELEMENTS);\n                    }\n                    catch (Exception e)\n                    {\n                        ForwardOnError(e);\n                    }\n                }\n            }\n        }\n    }\n\n    internal sealed class AverageSingle : Producer<float, AverageSingle._>\n    {\n        private readonly IObservable<float> _source;\n\n        public AverageSingle(IObservable<float> source)\n        {\n            _source = source;\n        }","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable/Average.cs#L41-L77","documentation":"An Average operator (integer variant) throws InvalidOperationException with Strings_Linq.NO_ELEMENTS when the source completes without emitting any value, because the average of an empty sequence is undefined. The operator forwards the error on OnCompleted instead of producing a result.","triggerScenarios":"Subscribing to Observable.Average over an empty source: an empty int/long array, a stream fully filtered out, or a completed subject with no OnNext.","commonSituations":"Averaging sensor or metrics streams that produced no samples; averaging query results that came back empty; tests that forgot to push values before completion.","solutions":["Use the nullable overload Observable.Average(source.Select(x => (int?)x)) which yields null for empty sequences","Catch the error: source.Average().Catch(Observable.Return(0.0)) or supply a default via Materialize/Dematerialize","Ensure the source emits at least one element before completing"],"exampleFix":"// before\nsource.Average().Subscribe(avg => ...); // throws on empty\n// after\nsource.Select(x => (int?)x).Average().Subscribe(avg => HandleAvg(avg ?? 0));","handlingStrategy":"try-catch","validationCode":"bool isEmpty = true; source.Subscribe(_ => isEmpty = false, () => { if (isEmpty) avgSubject.OnNext(0); });","typeGuard":null,"tryCatchPattern":"source.Average()\n    .Catch<double>(ex => ex is InvalidOperationException ? Observable.Return(0.0) : Observable.Throw<double>(ex))\n    .Subscribe(avg => ...);","preventionTips":["Use the nullable-element Average overload when the source may be empty","Validate that sources emit data before completion in integration tests","Supply sensible defaults (0, null) for empty aggregates at the call site"],"tags":["empty-sequence","average","invalidoperation","rx"],"backgroundTag":"empty-result-set","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"}