{"record":{"id":"632723b1aff8e3a0","repo":"dotnet/reactive","slug":"nameof-index","errorCode":null,"errorMessage":"nameof(index)","messagePattern":"nameof\\(index\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/ElementAt.cs","lineNumber":16,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information. \n\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TSource> ElementAt<TSource>(this IAsyncObservable<TSource> source, int index)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (index < 0)\n                throw new ArgumentOutOfRangeException(nameof(index));\n\n            return Create(\n                source,\n                index,\n                static (source, index, observer) => source.SubscribeSafeAsync(AsyncObserver.ElementAt(observer, index)));\n        }\n    }\n\n    public partial class AsyncObserver\n    {\n        public static IAsyncObserver<TSource> ElementAt<TSource>(IAsyncObserver<TSource> observer, int index)\n        {\n            if (observer == null)\n                throw new ArgumentNullException(nameof(observer));\n            if (index < 0)\n                throw new ArgumentOutOfRangeException(nameof(index));\n\n            return Create<TSource>(","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/ElementAt.cs#L1-L34","documentation":"Thrown by ElementAt(source, index) when index is negative. The operator throws ArgumentOutOfRangeException naming index because a negative position can never match an element. Unlike ElementAtOrDefault, ElementAt treats both a null source and a negative index as hard errors.","triggerScenarios":"Calling ElementAt(source, -1) or with an index computed from an unseeded counter, a failed parse, or an off-by-one decrement that went below zero.","commonSituations":"User-supplied indices parsed from config or CLI without validation; loop variables decremented before the call; arithmetic on empty collections (last - 1) yielding -1.","solutions":["Validate index >= 0 before calling ElementAt","Clamp negative computed values to 0 or skip the call when index < 0","Use ElementAtOrDefault for callers that want a default instead of an exception for missing positions (still validate non-negative first)","When the index comes from external input (config, CLI, HTTP), parse and range-check it explicitly"],"exampleFix":"// before\nint idx = items.Length - 1 - offset; // can be -1\nvar item = AsyncObservable.ElementAt(source, idx); // throws when idx < 0\n// after\nint idx = items.Length - 1 - offset;\nif (idx >= 0)\n{\n    var item = AsyncObservable.ElementAt(source, idx);\n}","handlingStrategy":"validation","validationCode":"if (index < 0) throw new InvalidOperationException(\"ElementAt requires a non-negative index\");\nvar item = AsyncObservable.ElementAt(source, index);","typeGuard":"bool IsValidIndex(int i) => i >= 0;","tryCatchPattern":"try\n{\n    var item = AsyncObservable.ElementAt(source, index);\n}\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"index\")\n{\n    // negative index; fall back to a default value\n    item = default;\n}","preventionTips":["Range-check any externally supplied index (config, CLI, HTTP) before calling ElementAt","Use ElementAtOrDefault when absence of an element should yield a default rather than throw","Guard computed indices from decrements/offsets that can go negative","Clamp: index = Math.Max(0, index) when a non-negative position is guaranteed by domain rules"],"tags":["argument-out-of-range","asyncrx","elementat-operator","index-validation"],"backgroundTag":"argument-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"}