Every piece of fake data in Forged ultimately comes from aDocumentation 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>. Whether you call f.Text.Guid(), f.Random.Number<int>(), or f.Person.Username(), each module method returns a concrete subclass of Generator<T>. Generators are designed to be composed: every modifier method returns a new generator that wraps the previous one, forming a small pipeline that is evaluated lazily when Generate() is finally called.
The type hierarchy
IGenerator<T> is covariant (out T), which means a Generator<string> can be assigned to an IGenerator<object>. This is particularly useful for the generated faker properties, which are typed as IGenerator<T> so that derived types can be used where base types are expected.
Implicit conversion to T
Generator<T> defines an implicit conversion operator that calls Generate() behind the scenes:
T:
The implicit conversion is convenient for quick scripts and demos, but inside faker lambdas you should return the generator itself (not the value) so Forged can cache it and call
Generate() on every Get() invocation.The pipeline model
Generators wrap other generators. Each modifier method creates a new generator that holds a reference to its source and adds a transformation layer. None of these layers executes untilGenerate() is called:
Generator<List<string>> that, on each call to Generate(), produces 2–5 randomly sized pronounceable words each with its first letter capitalised.
Core modifier methods
EveryGenerator<T> exposes the following modifier methods regardless of its concrete type:
OrDefault — introduce nulls with a probability
OrDefault — introduce nulls with a probability
default(T?) with the given probability (0.0–1.0), otherwise delegates to the source generator. Useful for nullable reference type properties where null is a valid value.Or — substitute an alternative value
Or — substitute an alternative value
other with the given probability, otherwise delegates to the source generator. Unlike OrDefault, the fallback value is a concrete T you supply.Refine — transform the generated value
Refine — transform the generated value
TNew. Use Refine for any custom mapping that is not covered by a built-in modifier.Enumerable / Array / List / HashSet — produce collections
Enumerable / Array / List / HashSet — produce collections
Generate() on the source to build a collection of the requested size. The range overloads pick a random count between minLength and maxLength on every call.Memo — capture and replay a value within one Get() cycle
Memo — capture and replay a value within one Get() cycle
Memo solves the problem of needing to reference a generated value more than once in the same Get() call — for example, combining a first name and last name into a full name.- The method returns a
MemoGenerator<T>, which behaves exactly like the source generator but stores the last generated value inCurrentValue. - It also emits a
MemoValueGenerator<T>via theoutparameter, which simply replaysCurrentValuewithout re-generating.
out var does not work inside C# object initializers, declare the receivers before the faker:Cast — downcast to a derived type
Cast — downcast to a derived type
TOut. Returns null if the cast is not possible.