AnyDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/atulin/forged/llms.txt
Use this file to discover all available pages before exploring further.
Generator<T> in Forged can be promoted into a collection generator with a single method call. Rather than writing a loop yourself, you chain one of the built-in collection modifiers directly onto the generator pipeline. These modifiers respect the same seeded Random instance as every other generator in the faker, so collection length and element values are both reproducible when a seed is provided.
Core Collection Modifiers
The following methods are defined on theGenerator<T> base class and are available on every generator in Forged.
Fixed-Length Collections
Pass a single integer to produce a collection with exactly that many elements:| Method | Return type |
|---|---|
.Enumerable(int length) | Generator<IEnumerable<T>> |
.Array(int length) | Generator<T[]> |
.List(int length) | Generator<List<T>> |
.HashSet(int length) | Generator<HashSet<T>> |
Variable-Length Collections
Pass amin and max to let the length itself be randomized on each Get() call:
| Method | Return type |
|---|---|
.Enumerable(int min, int max) | Generator<IEnumerable<T>> |
.Array(int min, int max) | Generator<T[]> |
.List(int min, int max) | Generator<List<T>> |
.HashSet(int min, int max) | Generator<HashSet<T>> |
Examples
Converting IEnumerable<T> Generators
When you use .Enumerable() (or receive a Generator<IEnumerable<T>> from another step) and need a concrete collection type, Forged provides the following extension methods.
.AsList()
Converts a Generator<IEnumerable<T>> to a Generator<List<T>>:
.AsHashSet()
Converts a Generator<IEnumerable<T>> to a Generator<HashSet<T>>:
.AsDictionary<TKey, TValue>(keySelector, valueSelector)
Converts a Generator<IEnumerable<T>> to a Generator<Dictionary<TKey, TValue>> using two selector functions:
.AsDictionary() is an extension on Generator<IEnumerable<T>>. Always use .Enumerable() (not .List() or .Array()) as the upstream step — .List() and .Array() return Generator<List<T>> and Generator<T[]> respectively, and Generator<List<T>> is not the same type as Generator<IEnumerable<T>> even though List<T> implements IEnumerable<T>. If you need deduplication beforehand, chain .Refine() before calling .AsDictionary()..Shuffle()
Produces a Generator<IEnumerable<T>> whose elements are in a randomised order each time Generate() is called:
Random.Shuffle(Span<T>) on the underlying array, so it participates in seeded RNG like every other operation.
Using .Refine() for Inline Transformations
.Refine<TNew>(Func<T, TNew> refiner) transforms the value produced by a generator. It is particularly useful for post-processing collections:
.Refine() to convert between collection types when extension methods are not available:
Nullable Collections
Collection generators are themselvesGenerator<TCollection> instances and support the same nullable modifiers as any other generator. Use .OrDefault(float probability) to make the entire collection optionally null:
MiddleNames property.