{"record":{"id":"32e51ce01e61ef73","repo":"dotnet/reactive","slug":"index-observable-aggregates","errorCode":null,"errorMessage":"index","messagePattern":"index","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Aggregates.cs","lineNumber":739,"sourceCode":"        /// Returns the element at a specified index in a sequence.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Observable sequence to return the element from.</param>\n        /// <param name=\"index\">The zero-based index of the element to retrieve.</param>\n        /// <returns>An observable sequence that produces the element at the specified position in the source sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> is null.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"index\"/> is less than zero.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\">(Asynchronous) <paramref name=\"index\"/> is greater than or equal to the number of elements in the source sequence.</exception>\n        public static IObservable<TSource> ElementAt<TSource>(this IObservable<TSource> source, int index)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (index < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(index));\n            }\n\n            return s_impl.ElementAt(source, index);\n        }\n\n        #endregion\n\n        #region + ElementAtOrDefault +\n\n        /// <summary>\n        /// Returns the element at a specified index in a sequence or a default value if the index is out of range.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Observable sequence to return the element from.</param>\n        /// <param name=\"index\">The zero-based index of the element to retrieve.</param>\n        /// <returns>An observable sequence that produces the element at the specified position in the source sequence, or a default value if the index is outside the bounds of the source sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> is null.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"index\"/> is less than zero.</exception>","sourceCodeStart":721,"sourceCodeEnd":757,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Aggregates.cs#L721-L757","documentation":"ElementAt throws ArgumentOutOfRangeException because the index parameter is negative. The guard at Observable.Aggregates.cs:739 rejects index < 0 synchronously, mirroring LINQ-to-Objects ElementAt semantics. Element positions are zero-based, so 0 is the first element and negative values have no meaning.","triggerScenarios":"Calling source.ElementAt(index) with index < 0 — e.g. passing -1 as a 'not found' sentinel, computing the index via subtraction (lastIndex - removed) that underflows, or copying an index from a list search that returned -1.","commonSituations":"Using IndexOf/List.Find results (-1) directly as an ElementAt index; index arithmetic on empty collections where lastIndex becomes -1; off-by-one bugs when translating 1-based user input to 0-based indices.","solutions":["Clamp or validate the index before the call (Math.Max(0, index)).","If index came from a search operation, check for -1 (not found) before using it.","Use ElementAtOrDefault if you want a default value instead of an exception for out-of-range positions — but still guard negatives since it also throws for index < 0.","Fix the index arithmetic that produced the negative value."],"exampleFix":"// before\nvar idx = list.IndexOf(item);\nvar value = await source.ElementAt(idx); // idx == -1 when not found\n// after\nvar idx = list.IndexOf(item);\nvar value = idx >= 0 ? await source.ElementAt(idx) : defaultValue;","handlingStrategy":"validation","validationCode":"if (index < 0) throw new InvalidOperationException(\"ElementAt index must be non-negative\");","typeGuard":"bool IsValidIndex(int index) => index >= 0;","tryCatchPattern":"try { var item = await source.ElementAt(index); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"index\") { /* handle negative index */ }","preventionTips":["Check IndexOf/Find results for -1 before using them as indices","Clamp computed indices: Math.Max(0, index)","Convert 1-based user input with explicit (input - 1) and validate"],"tags":["dotnet","rx","argument-out-of-range","index"],"backgroundTag":"value-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"}