{"record":{"id":"ba149d3cc5145d21","repo":"dotnet/reactive","slug":"specified-argument-was-out-of-the-range-of-valid-values-ba149d","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'readerCount')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'readerCount'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Ix.NET/Source/System.Interactive/System/Linq/Operators/Memoize.cs","lineNumber":81,"sourceCode":"        /// Creates a buffer with a view over the source sequence, causing a specified number of enumerators to obtain access\n        /// to all of the sequence's elements without causing multiple enumerations over the source.\n        /// </summary>\n        /// <typeparam name=\"TSource\">Source sequence element type.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"readerCount\">\n        /// Number of enumerators that can access the underlying buffer. Once every enumerator has\n        /// obtained an element from the buffer, the element is removed from the buffer.\n        /// </param>\n        /// <returns>\n        /// Buffer enabling a specified number of enumerators to retrieve all elements from the shared source sequence,\n        /// without duplicating source enumeration side-effects.\n        /// </returns>\n        public static IBuffer<TSource> Memoize<TSource>(this IEnumerable<TSource> source, int readerCount)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (readerCount <= 0)\n                throw new ArgumentOutOfRangeException(nameof(readerCount));\n\n            return new MemoizedBuffer<TSource>(source.GetEnumerator(), readerCount);\n        }\n\n        /// <summary>\n        /// Memoizes the source sequence within a selector function where a specified number of enumerators can get access to\n        /// all of the sequence's elements without causing multiple enumerations over the source.\n        /// </summary>\n        /// <typeparam name=\"TSource\">Source sequence element type.</typeparam>\n        /// <typeparam name=\"TResult\">Result sequence element type.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"readerCount\">\n        /// Number of enumerators that can access the underlying buffer. Once every enumerator has\n        /// obtained an element from the buffer, the element is removed from the buffer.\n        /// </param>\n        /// <param name=\"selector\">\n        /// Selector function with memoized access to the source sequence for a specified number of\n        /// enumerators.","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Ix.NET/Source/System.Interactive/System/Linq/Operators/Memoize.cs#L63-L99","documentation":"Memoize(source, readerCount) validates that readerCount is a positive integer. When readerCount is zero or negative, the library throws ArgumentOutOfRangeException naming 'readerCount'. The reader count is the number of enumerators allowed to independently consume the memoized buffer, so a non-positive value is meaningless.","triggerScenarios":"Calling EnumerableEx.Memoize(source, readerCount) with readerCount <= 0 (e.g. Memoize(seq, 0) or Memoize(seq, -1)), often when readerCount is computed from a variable that defaults to 0 or comes from unparsed/failed configuration.","commonSituations":"Passing a default-initialized int (0) before setting the desired reader count; parsing a config value that failed and yielded 0; misreading the parameter as a capacity or buffer size and passing 0 for 'unlimited'.","solutions":["Pass a positive readerCount (>= 1) matching the number of concurrent enumerators you expect.","Validate/clamp the value before calling: if (readerCount < 1) readerCount = 1;","If you don't need a bounded reader count, use the parameterless Memoize(source) overload."],"exampleFix":"// before\nvar buffer = source.Memoize(readerCount); // readerCount is 0\n// after\nif (readerCount < 1) readerCount = 1;\nvar buffer = source.Memoize(readerCount);","handlingStrategy":"validation","validationCode":"if (readerCount < 1) throw new ArgumentOutOfRangeException(nameof(readerCount), \"Must be a positive integer.\");\nvar buffer = source.Memoize(readerCount);","typeGuard":"static bool IsValidReaderCount(int n) => n >= 1;","tryCatchPattern":"try { buffer = source.Memoize(readerCount); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"readerCount\") { buffer = source.Memoize(1); }","preventionTips":["Never pass 0 or negative reader counts; default to 1 or the parameterless overload","Clamp computed values with Math.Max(1, n)","Beware int fields defaulting to 0 used as reader counts"],"tags":["argument-out-of-range","linq","ix-interactive","memoize"],"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"}