Class OptionalExtensions
- Namespace
- Auth0.ManagementApi.Core
- Assembly
- Auth0.ManagementApi.dll
public static class OptionalExtensions
- Inheritance
-
OptionalExtensions
- Inherited Members
Methods
AddIfNotNull<T>(T?, Dictionary<string, object?>, string)
Adds a nullable value type to a dictionary only if it has a value. This is useful for regular nullable properties where null means "omit from request".
public static void AddIfNotNull<T>(this T? value, Dictionary<string, object?> dictionary, string key) where T : struct
Parameters
valueT?The nullable value to add.
dictionaryDictionary<string, object>The dictionary to add to.
keystringThe key to use in the dictionary.
Type Parameters
TThe underlying value type.
Examples
var dict = new Dictionary<string, object?>();
request.Age.AddIfNotNull(dict, "age"); // Only adds if HasValue
request.Score.AddIfNotNull(dict, "score"); // Only adds if HasValue
AddIfNotNull<T>(T?, Dictionary<string, object?>, string)
public static void AddIfNotNull<T>(this T? value, Dictionary<string, object?> dictionary, string key) where T : class
Parameters
valueTdictionaryDictionary<string, object>keystring
Type Parameters
T
AddTo<T>(Optional<T>, Dictionary<string, object?>, string)
Adds the value to a dictionary if the optional is defined (even if the value is null). This is useful for building JSON request payloads where null values should be included.
public static void AddTo<T>(this Optional<T> optional, Dictionary<string, object?> dictionary, string key)
Parameters
optionalOptional<T>The optional value to add.
dictionaryDictionary<string, object>The dictionary to add to.
keystringThe key to use in the dictionary.
Type Parameters
TThe type of the optional value.
Examples
var dict = new Dictionary<string, object?>();
request.Name.AddTo(dict, "name"); // Adds only if Name.IsDefined
request.Email.AddTo(dict, "email"); // Adds only if Email.IsDefined
IfDefined<T>(Optional<T>, Action<T>)
Executes an action if the optional is defined.
public static void IfDefined<T>(this Optional<T> optional, Action<T> action)
Parameters
Type Parameters
TThe type of the optional value.
Examples
request.Name.IfDefined(name => Console.WriteLine($"Name: {name}"));
Map<T, TResult>(Optional<T>, Func<T, TResult>)
Maps the value to a new type if the optional is defined, otherwise returns undefined.
public static Optional<TResult> Map<T, TResult>(this Optional<T> optional, Func<T, TResult> mapper)
Parameters
Returns
- Optional<TResult>
An optional containing the mapped value if defined, otherwise undefined.
Type Parameters
TThe type of the original value.
TResultThe type to map to.
Examples
Optional<string?> name = Optional<string?>.Of("John");
Optional<int> length = name.Map(n => n?.Length ?? 0); // Optional.Of(4)