Skip to content

OnixLabs.Core.ObjectExtensions

Matthew Layton edited this page Nov 15, 2026 · 3 revisions

ObjectExtensions Class

Extension methods for object instances, including functional pipe helpers, record-style string formatting, and conversions to Optional<T> and Result<T>.

Namespace OnixLabs.Core
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → ObjectExtensions

Definition

[EditorBrowsable(EditorBrowsableState.Never)]
public static class ObjectExtensions

Note This class is decorated with [EditorBrowsable(EditorBrowsableState.Never)], which keeps the container class itself out of IntelliSense lists while its extension methods remain fully usable on the receiver types. Bring the methods into scope with using OnixLabs.Core;.

Remarks

ObjectExtensions provides functional-style pipe helpers (Apply, Let), null-safe string formatting (ToRecordString, ToStringOrNull), null extraction (TryGetNonNull), and conversions to the Optional<T> and Success<T> result types — including their async (Task<T>) counterparts. The async overloads live alongside their synchronous siblings in the same class.

Methods

Method Returns Description
Apply<T>(this T, Action<T>) T Invokes an action with the current reference-type value and returns the value.
Apply<T>(this T, Func<T, T>) T Invokes a function with the current value-type value and returns its result.
Let<TSource, TResult>(this TSource, Func<TSource, TResult>) TResult Invokes a function with the current value and returns its result.
ToRecordString(this object?) string Gets a record-like string representation of the current object.
ToStringOrNull(this object?) string Gets a string representation, or the literal "null" when the object is null.
ToOptional<T>(this T?) (class) Optional<T> Wraps a reference-type value as an Optional<T>.
ToOptional<T>(this T?) (struct) Optional<T> Wraps a value-type value as an Optional<T>.
ToOptionalAsync<T>(this Task<T?>, CancellationToken) (class) Task<Optional<T>> Asynchronously wraps a reference-type value as an Optional<T>.
ToOptionalAsync<T>(this Task<T?>, CancellationToken) (struct) Task<Optional<T>> Asynchronously wraps a value-type value as an Optional<T>.
ToSuccess<T>(this T) Success<T> Wraps the current value as a Success<T> result.
ToSuccessAsync<T>(this Task<T>, CancellationToken) Task<Success<T>> Asynchronously wraps the current value as a Success<T> result.
TryGetNonNull<T>(this T?, out T) bool Attempts to extract a non-null value from the current nullable object.

Apply<T>(this T, Action<T>)

public static T Apply<T>(this T value, Action<T> action) where T : class

Calls the specified Action<T> with the current value, then returns the value unchanged. Useful for configuring a reference-type object in a fluent chain.

Parameter Type Description
value T The value to pass to the action.
action Action<T> The action into which value is passed.

Returns — The current value, after the action has executed.

Exceptions

Exception Condition
ArgumentNullException action is null.
var list = new List<int>().Apply(items => items.Add(1)).Apply(items => items.Add(2));
// list contains { 1, 2 }

Apply<T>(this T, Func<T, T>)

public static T Apply<T>(this T value, Func<T, T> function) where T : struct

Calls the specified Func<T, T> with the current value-type value and returns its result. This overload is constrained to value types.

Parameter Type Description
value T The value to pass to the function.
function Func<T, T> The function into which value is passed.

Returns — The result of function.

Exceptions

Exception Condition
ArgumentNullException function is null.
int result = 10.Apply(value => value * 2); // 20

Let<TSource, TResult>(this TSource, Func<TSource, TResult>)

public static TResult Let<TSource, TResult>(this TSource value, Func<TSource, TResult> function)

Calls the specified Func<TSource, TResult> with the current value as its argument and returns the result, transforming the value into a new type.

Parameter Type Description
value TSource The value to pass to the function.
function Func<TSource, TResult> The function into which value is passed.

Returns — The result of function.

Exceptions

Exception Condition
ArgumentNullException function is null.
int length = "Hello".Let(text => text.Length); // 5

ToRecordString(this object?)

public static string ToRecordString(this object? value)

Gets a record-like string representation of the current object instance, listing its public instance properties as Name = Value pairs inside braces. Returns the literal "null" when value is null.

Note This method is designed specifically for record-like objects and may produce undesirable results when applied to primitive-like objects.

Parameter Type Description
value object? The current object instance.

Returns — A record-like string representation of the current object instance.

var point = new { X = 1, Y = 2 };
string text = point.ToRecordString(); // e.g. "<>f__AnonymousType { X = 1, Y = 2 }"

ToStringOrNull(this object?)

public static string ToStringOrNull(this object? value)

Obtains a string representation of the current object, or the string literal "null" if the current object is null.

Parameter Type Description
value object? The current object instance.

Returns — The result of value.ToString(), or "null" when value is null.

ToOptional<T>(this T?) (class)

public static Optional<T> ToOptional<T>(this T? value) where T : notnull

Obtains an Optional<T> representation of the current reference-type object.

Parameter Type Description
value T? The object to wrap as an Optional<T> value.

Returns — An Optional<T> representation of the current object.

Optional<string> some = "value".ToOptional();
Optional<string> none = ((string?)null).ToOptional();

ToOptional<T>(this T?) (struct)

public static Optional<T> ToOptional<T>(this T? value) where T : struct

Obtains an Optional<T> representation of the current value-type object.

Parameter Type Description
value T? The object to wrap as an Optional<T> value.

Returns — An Optional<T> representation of the current object.

Optional<int> some = ((int?)42).ToOptional();

ToOptionalAsync<T>(this Task<T?>, CancellationToken) (class)

public static async Task<Optional<T>> ToOptionalAsync<T>(this Task<T?> value, CancellationToken cancellationToken = default)
    where T : notnull

Asynchronously obtains an Optional<T> representation of the current reference-type object produced by the awaited task.

Parameter Type Description
value Task<T?> The task whose result is wrapped as an Optional<T> value.
cancellationToken CancellationToken The token to monitor for cancellation requests.

Returns — A task that produces an Optional<T> representation of the awaited result.

ToOptionalAsync<T>(this Task<T?>, CancellationToken) (struct)

public static async Task<Optional<T>> ToOptionalAsync<T>(this Task<T?> value, CancellationToken cancellationToken = default)
    where T : struct

Asynchronously obtains an Optional<T> representation of the current value-type object produced by the awaited task.

Parameter Type Description
value Task<T?> The task whose result is wrapped as an Optional<T> value.
cancellationToken CancellationToken The token to monitor for cancellation requests.

Returns — A task that produces an Optional<T> representation of the awaited result.

ToSuccess<T>(this T)

public static Success<T> ToSuccess<T>(this T value)

Obtains a Success<T> representation of the current object.

Parameter Type Description
value T The object to wrap as a Success<T> result.

Returns — A Success<T> representation of the current object.

Success<int> success = 42.ToSuccess();

ToSuccessAsync<T>(this Task<T>, CancellationToken)

public static async Task<Success<T>> ToSuccessAsync<T>(this Task<T> value, CancellationToken cancellationToken = default)

Asynchronously obtains a Success<T> representation of the value produced by the awaited task.

Parameter Type Description
value Task<T> The task whose result is wrapped as a Success<T> result.
cancellationToken CancellationToken The token to monitor for cancellation requests.

Returns — A task that produces a Success<T> representation of the awaited result.

TryGetNonNull<T>(this T?, out T)

public static bool TryGetNonNull<T>(this T? value, [NotNullWhen(true)] out T result)

Attempts to extract a non-null value from the current nullable object. When the method returns true, result is guaranteed non-null (annotated with [NotNullWhen(true)]).

Parameter Type Description
value T? The current nullable object to test for nullability.
result out T The non-null value when this method returns true; otherwise, null.

Returns — true if the current nullable object is not null; otherwise, false.

string? maybe = GetValueOrNull();

if (maybe.TryGetNonNull(out string value))
{
    Console.WriteLine(value.Length);
}

See Also

ONIXLabs .NET Library


Clone this wiki locally