{"record":{"id":"226613555a6c4f68","repo":"bchavez/Bogus","slug":"the-collection-is-empty-there-are-no-items-to-sel","errorCode":null,"errorMessage":"The collection is empty. There are no items to select.","messagePattern":"The collection is empty\\. There are no items to select\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/Randomizer.cs","lineNumber":571,"sourceCode":"   }\n\n   /// <summary>\n   /// Get a random subset of a List.\n   /// </summary>\n   /// <param name=\"items\">The source of items to pick from.</param>\n   /// <param name=\"count\">The number of items to pick; otherwise, a random amount is picked.</param>\n   public IList<T> ListItems<T>(List<T> items, int? count = null)\n   {\n      return ListItems(items as IList<T>, count);\n   }\n\n   /// <summary>\n   /// Get a random collection item.\n   /// </summary>\n   public T CollectionItem<T>(ICollection<T> collection)\n   {\n      if( collection.Count <= 0 )\n         throw new ArgumentException(\"The collection is empty. There are no items to select.\", nameof(collection));\n\n      var r = Number(max: collection.Count - 1);\n      return collection.Skip(r).First();\n   }\n\n   /// <summary>\n   /// Replaces symbols with numbers.\n   /// IE: ### -> 283\n   /// </summary>\n   /// <param name=\"format\">The string format</param>\n   /// <param name=\"symbol\">The symbol to search for in format that will be replaced with a number</param>\n   public string ReplaceNumbers(string format, char symbol = '#')\n   {\n      return ReplaceSymbols(format, symbol, () => Convert.ToChar('0' + Number(9)));\n   }\n\n   /// <summary>\n   /// Replaces each character instance in a string.","sourceCodeStart":553,"sourceCodeEnd":589,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/Randomizer.cs#L553-L589","documentation":"Thrown by Randomizer.CollectionItem<T> when collection.Count <= 0. The method uses Skip(r).First() so it needs at least one element. The param is named 'collection'.","triggerScenarios":"Calling f.Random.CollectionItem(new HashSet<T>()) or any ICollection<T> with zero elements.","commonSituations":"Passing an empty set/dictionary-keys collection; results of a where-filter that produced nothing.","solutions":["Check collection.Count > 0 before calling CollectionItem.","Populate or supply a fallback collection.","Guard upstream filters that can yield empty results."],"exampleFix":"// before\nvar v = f.Random.CollectionItem(set);\n// after\nvar v = set.Count > 0 ? f.Random.CollectionItem(set) : defaultValue;","handlingStrategy":"validation","validationCode":"static T SafeCollectionItem<T>(Bogus.Randomizer r, ICollection<T> c, T fallback) {\n    return c.Count > 0 ? r.CollectionItem(c) : fallback;\n}","typeGuard":"bool HasItems<T>(ICollection<T> c) => c.Count > 0;","tryCatchPattern":"try { return r.CollectionItem(c); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"collection is empty\")) {\n    return default;\n}","preventionTips":["Check collection.Count before sampling.","Guard filters that can yield empty collections.","Provide a non-empty fallback collection."],"tags":["bogus","randomizer","collectionitem","empty-collection"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}