{"record":{"id":"bbf22a2549539805","repo":"restsharp/RestSharp","slug":"value-must-be-non-negative","errorCode":null,"errorMessage":"value must be non-negative","messagePattern":"value must be non-negative","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/RestSharp/Polyfills/Index.cs","lineNumber":31,"sourceCode":"/// Index is used by the C# compiler to support the new index syntax\n/// <code>\n/// int[] someArray = new int[5] { 1, 2, 3, 4, 5 } ;\n/// int lastElement = someArray[^1]; // lastElement = 5\n/// </code>\n/// </remarks>\nreadonly struct Index : IEquatable<Index> {\n    readonly int _value;\n\n    /// <summary>Construct an Index using a value and indicating if the index is from the start or from the end.</summary>\n    /// <param name=\"value\">The index value. it has to be zero or positive number.</param>\n    /// <param name=\"fromEnd\">Indicating if the index is from the start or from the end.</param>\n    /// <remarks>\n    /// If the Index constructed from the end, index value 1 means pointing at the last element and index value 0 means pointing at beyond last element.\n    /// </remarks>\n    [MethodImpl(MethodImplOptions.AggressiveInlining)]\n    public Index(int value, bool fromEnd = false) {\n        if (value < 0) {\n            throw new ArgumentOutOfRangeException(nameof(value), \"value must be non-negative\");\n        }\n\n        if (fromEnd)\n            _value = ~value;\n        else\n            _value = value;\n    }\n\n    // The following private constructors mainly created for perf reason to avoid the checks\n    Index(int value) => _value = value;\n\n    /// <summary>Create an Index pointing at first element.</summary>\n    public static Index Start => new(0);\n\n    /// <summary>Create an Index pointing at beyond last element.</summary>\n    public static Index End => new(~0);\n\n    /// <summary>Create an Index from the start at the position indicated by the value.</summary>","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/restsharp/RestSharp/blob/6a5082169257438cd085f822f050d93256a8e499/src/RestSharp/Polyfills/Index.cs#L13-L49","documentation":"Thrown by the Index polyfill constructor on older target frameworks (netstandard2.0, net471, net48) where System.Index is not built-in. The Index struct backs the C# range/index syntax (e.g. array[^1]). The constructor rejects negative values because an index position cannot be negative. It mirrors the contract of the official .NET runtime Index type.","triggerScenarios":"Calling new Index(value, fromEnd) or implicitly converting an int to Index where the int is negative. In RestSharp internals this is reached when compiled index/range expressions (like assembled[1..] or AbsoluteUri[^1]) resolve to a negative computed position. Direct construction with a negative value also triggers it.","commonSituations":"Running RestSharp on netstandard2.0/net4x where the polyfill is active, combined with an edge-case string/URI that is empty or has unexpected length, so a computed index like str[^1] effectively resolves through a negative intermediate. Rare for end users; mostly a library-internal invariant guard.","solutions":["Upgrade the consuming project to a modern TFM (net8.0/net9.0) so the built-in System.Index is used and this polyfill is not compiled.","Ensure any inputs that feed into string slicing (BaseUrl, Resource, ContentType values) are non-empty before they reach RestSharp's URI/content assembly code.","If reproducing directly, verify the value passed to new Index is >= 0 before construction."],"exampleFix":"// before\nvar ch = baseUrl.AbsoluteUri[^1]; // AbsoluteUri empty -> negative index path\n\n// after\nif (!string.IsNullOrEmpty(baseUrl.AbsoluteUri)) {\n    var ch = baseUrl.AbsoluteUri[^1];\n}","handlingStrategy":"validation","validationCode":"// Validate before any index/range operation that could go negative on legacy TFMs\nint value = ComputeIndex();\nif (value < 0) throw new ArgumentOutOfRangeException(nameof(value), \"Index must be non-negative\");\nvar index = new Index(value);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Target net8.0+ so the built-in System.Index is used and the polyfill is not compiled.","Never feed empty strings into code that slices with [^n] or [n..].","Treat negative computed indices as a bug in upstream length/offset calculation."],"tags":["argument","polyfill","index","netstandard","validation"],"backgroundTag":null,"analyzedSha":"6a5082169257438cd085f822f050d93256a8e499","analyzedAt":"2026-08-13T20:38:25.807Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}