Table of Contents

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

value T?

The nullable value to add.

dictionary Dictionary<string, object>

The dictionary to add to.

key string

The key to use in the dictionary.

Type Parameters

T

The 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

value T
dictionary Dictionary<string, object>
key string

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

optional Optional<T>

The optional value to add.

dictionary Dictionary<string, object>

The dictionary to add to.

key string

The key to use in the dictionary.

Type Parameters

T

The 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

optional Optional<T>

The optional value.

action Action<T>

The action to execute with the value.

Type Parameters

T

The 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

optional Optional<T>

The optional value to map.

mapper Func<T, TResult>

The mapping function.

Returns

Optional<TResult>

An optional containing the mapped value if defined, otherwise undefined.

Type Parameters

T

The type of the original value.

TResult

The type to map to.

Examples

Optional<string?> name = Optional<string?>.Of("John");
Optional<int> length = name.Map(n => n?.Length ?? 0);  // Optional.Of(4)