C# distinguishes sharply between nullable reference types (e.g.,Documentation 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.
string?) and nullable value types (e.g., DateTime?, int?). Forged mirrors this distinction with separate extension methods for each case, letting you introduce null or alternative values into any generator pipeline with a single fluent call and a probability float.
Value Types: .OrNull() and .Nullable()
Structs (value types) cannot be null on their own — they must be wrapped in Nullable<T> (written as T?). Forged provides two methods on any Generator<T> where T : struct.
.OrNull(float probability)
Returns null with the given probability (0.0–1.0), otherwise delegates to the inner generator. The return type becomes Generator<T?>.
.Nullable()
Converts Generator<T> (struct) to Generator<T?> with zero probability of null — it simply lifts the value type into the nullable wrapper. This is useful when your model property is declared as T? but you always want a value:
.OrNull() and .Nullable() are only available when T is a struct (value type). For nullable reference types such as string? or List<T>?, use .OrDefault() instead.Reference Types: .OrDefault()
Reference types are already nullable in C# — string? and string are the same runtime type. To make a generator occasionally return null for a reference-type property, use .OrDefault(float probability), which returns default(T?) (i.e., null) with the specified probability.
.OrDefault() is defined on the base Generator<T> class and works for both reference types and value types (returning the type’s default, which for structs is default(T) — the zero-value, not null).
Any Type: .Or(T other, float probability)
.Or() is not specifically about null — it returns an arbitrary alternative value with the specified probability, otherwise it returns the generated value. The type stays T (non-nullable):
Optional (Omitted) Properties
Any non-required property in a Forged-generated faker is optional in the initializer. If you omit it entirely, the model property keeps its default value — null for nullable types, false for bool, 0 for numeric types, etc. No generator runs for that property at all.
Choosing the Right API
- Value types (structs)
- Reference types
- Alternative values
- Optional properties
Use
.OrNull() when you want a mix of values and nulls, or .Nullable() when you always want a value but the property is typed as T?:Quick Reference
| Scenario | API | Return type |
|---|---|---|
| Struct property, sometimes null | .OrNull(float probability) | Generator<T?> |
Struct property, always a value but typed T? | .Nullable() | Generator<T?> |
| Reference-type property, sometimes null | .OrDefault(float probability) | Generator<T?> |
| Any type, sometimes a specific fallback | .Or(T other, float probability) | Generator<T> |
| Non-required property, always default | Omit from faker initializer | default(T) |
Complete Example
The model below mixesrequired properties, optional-but-configured properties, and entirely omitted properties: