Every Forged generator inherits from the abstractDocumentation 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> base class, which gives you a rich set of fluent modifier methods out of the box. These methods let you introduce optional values, transform outputs into new types, produce entire collections from a single generator, and capture generated values for reuse — all without breaking the composable pipeline style that Forged is built around.
Generator<T> defines an implicit conversion to T that calls Generate() under the hood. This means you can assign a generator directly anywhere a T is expected, and the value is produced on the spot. All modifier methods return new generator instances — they never mutate the original.Conditional / fallback
.Or(T other, float probability)
Returns a new generator that produces other with the given probability instead of calling the inner generator.
The alternative value to return when the probability condition is met.
A value between
0.0 and 1.0. At 0.0 the fallback is never returned; at 1.0 it is always returned.Generator<T>
.OrDefault(float probability)
Returns a new generator that produces default(T?) with the given probability instead of calling the inner generator. Useful for introducing nullable gaps in test data.
A value between
0.0 and 1.0 representing the chance of returning the default value.Generator<T?>
Transformation
.Refine<TNew>(Func<T, TNew> refiner)
Applies a transformation function to every generated value, producing a new generator of a different type. This is the fundamental “map” operation in a Forged pipeline.
A pure function that receives the generated
T value and returns a TNew value.Generator<TNew>
.Cast<TOut>() where TOut : T
Casts the generated value to TOut, where TOut must be a subtype of T (enforced by the where TOut : T constraint). Returns a LiteralGenerator wrapping the result of a single Generate() call. Returns null if the runtime cast is not possible.
Type constraint where TOut : T — TOut must derive from or implement T.
Returns IGenerator<TOut?>
Cast<TOut> eagerly calls Generate() once at construction time; the resulting IGenerator<TOut?> always produces that same captured value. For lazy casts, prefer .Refine(x => (TOut?)x).Collection factories
All collection factory methods are built on the sameEnumerableGenerator<T> infrastructure. The overloads accepting both min and max pick a random length in that inclusive range on every Generate() call.
.Enumerable(int length) and .Enumerable(int min, int max)
The exact number of elements to generate.
The minimum number of elements (inclusive).
The maximum number of elements (inclusive).
Generator<IEnumerable<T>>
.Array(int length) and .Array(int min, int max)
The exact number of elements to generate.
The minimum number of elements (inclusive).
The maximum number of elements (inclusive).
Generator<T[]>
.List(int length) and .List(int min, int max)
The exact number of elements to generate.
The minimum number of elements (inclusive).
The maximum number of elements (inclusive).
Generator<List<T>>
.HashSet(int length) and .HashSet(int min, int max)
The exact number of elements to generate.
The minimum number of elements (inclusive).
The maximum number of elements (inclusive).
Generator<HashSet<T>>
Because
HashSet<T> deduplicates elements, the actual number of items in the set may be less than the requested length if the inner generator produces duplicate values.Memoization
.Memo(out MemoValueGenerator<T> memoizedValue)
Captures the value produced by the current generator so it can be referenced elsewhere in the same faker configuration. Calling .Memo() returns a MemoGenerator<T> that, when generated, stores its output in an internal field. The out parameter provides a companion MemoValueGenerator<T> that always returns that same stored value without re-running the inner generator.
An
out parameter that receives the companion reader generator. Assign this to a field or local variable and reference it in other properties of the same faker.MemoGenerator<T> — a generator that both produces the value and caches it for the companion to read.
createdAt is a MemoValueGenerator<DateTime>. Every time an Order is generated, CreatedAt is picked first, stored in the memo, then UpdatedAt uses it as a lower bound.
String conversion (from GeneratorExtensions)
These two methods are defined as static extension methods in GeneratorExtensions and are available on any Generator<T>.
.ToString()
Calls .ToString() on each generated value using the default object.ToString() implementation.
Returns Generator<string?> — the result is nullable because object.ToString() can theoretically return null.
.ToString(string format, CultureInfo? cultureInfo)
Formats each generated value using the supplied format string. Only available when T implements ISpanFormattable (e.g. DateTime, int, double, Guid, …).
A standard or custom format string accepted by
ISpanFormattable.ToString.The culture to use for formatting. Defaults to the generator’s
Forge locale when null.Generator<string> — always non-null.