{"record":{"id":"e23509e96f687568","repo":"dotnet/reactive","slug":"index","errorCode":null,"errorMessage":"index","messagePattern":"index","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable/ElementAt.cs","lineNumber":49,"sourceCode":"\n            public override void OnNext(TSource value)\n            {\n                if (_i == 0)\n                {\n                    ForwardOnNext(value);\n                    ForwardOnCompleted();\n                }\n\n                _i--;\n            }\n\n            public override void OnCompleted()\n            {\n                if (_i >= 0)\n                {\n                    try\n                    {\n                        throw new ArgumentOutOfRangeException(\"index\");\n                    }\n                    catch (Exception e)\n                    {\n                        ForwardOnError(e);\n                    }\n                }\n            }\n        }\n    }\n}\n","sourceCodeStart":31,"sourceCodeEnd":60,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable/ElementAt.cs#L31-L60","documentation":"Observable.ElementAt's observer tracks whether the requested index was seen; if the source completes before emitting index+1 elements, OnCompleted throws ArgumentOutOfRangeException(\"index\") and forwards it via OnError, since the requested position does not exist.","triggerScenarios":"Calling Observable.ElementAt(source, i) where the source emits fewer than i+1 elements before completing; also negative indices hit the same path.","commonSituations":"Indexing into a stream based on user input without knowing sequence length; requesting element 5 from a stream that yields only 2 values.","solutions":["Use ElementAtOrDefault which emits default(T) instead of erroring when the index is out of range","Validate the known length of the sequence before requesting an index","Catch ArgumentOutOfRangeException in OnError and provide a fallback"],"exampleFix":"// before\nsource.ElementAt(5).Subscribe(x => ..., ex => throw ex);\n// after\nsource.ElementAtOrDefault(5).Subscribe(x => Console.WriteLine(x));","handlingStrategy":"try-catch","validationCode":"if (index < 0) throw new ArgumentOutOfRangeException(nameof(index));\n// only use ElementAt when the sequence is known to have index+1 elements","typeGuard":null,"tryCatchPattern":"source.ElementAt(i).Subscribe(\n    x => Console.WriteLine(x),\n    ex => { if (ex is ArgumentOutOfRangeException) Console.WriteLine(default); else throw ex; });","preventionTips":["Prefer ElementAtOrDefault for out-of-range safety","Don't index blindly into streams of unknown length","Reject negative indices before subscribing"],"tags":["reactive","linq","elementat","argument-out-of-range"],"backgroundTag":"index-out-of-range","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"}