Table of Contents

Class ObjectUtils

Namespace
Utils.Objects
Assembly
Utils.dll

Exposes null-aware control flow helpers and hash code utilities for arbitrary objects.

public static class ObjectUtils
Inheritance
ObjectUtils
Inherited Members

Methods

Between<T>(T, IComparer<T>, T, T)

Determines whether a value is between a lower and upper bound using a custom comparer (inclusive).

public static bool Between<T>(this T value, IComparer<T> comparer, T lowerBound, T upperBound)

Parameters

value T

The value to check.

comparer IComparer<T>

The custom comparer to use.

lowerBound T

The lower bound.

upperBound T

The upper bound.

Returns

bool

True if the value is between the bounds; otherwise, false.

Type Parameters

T

The type of the value.

Between<T>(T, IComparer<T>, T, T, bool, bool)

Determines whether a value is between a lower and upper bound using a custom comparer with optional inclusivity.

public static bool Between<T>(this T value, IComparer<T> comparer, T lowerBound, T upperBound, bool includeLowerBound, bool includeUpperBound)

Parameters

value T

The value to check.

comparer IComparer<T>

The custom comparer to use.

lowerBound T

The lower bound.

upperBound T

The upper bound.

includeLowerBound bool

Whether to include the lower bound in the comparison.

includeUpperBound bool

Whether to include the upper bound in the comparison.

Returns

bool

True if the value is between the bounds with the specified inclusivity; otherwise, false.

Type Parameters

T

The type of the value.

Between<T>(T, T, T)

Determines whether a value is between a lower and upper bound (inclusive).

public static bool Between<T>(this T value, T lowerBound, T upperBound) where T : IComparable<T>

Parameters

value T

The value to check.

lowerBound T

The lower bound.

upperBound T

The upper bound.

Returns

bool

True if the value is between the bounds; otherwise, false.

Type Parameters

T

The type of the value.

Between<T>(T, T, T, bool, bool)

Determines whether a value is between a lower and upper bound with optional inclusivity.

public static bool Between<T>(this T value, T lowerBound, T upperBound, bool includeLowerBound = true, bool includeUpperBound = true) where T : IComparable<T>

Parameters

value T

The value to check.

lowerBound T

The lower bound.

upperBound T

The upper bound.

includeLowerBound bool

Whether to include the lower bound in the comparison.

includeUpperBound bool

Whether to include the upper bound in the comparison.

Returns

bool

True if the value is between the bounds with the specified inclusivity; otherwise, false.

Type Parameters

T

The type of the value.

ComputeHash(Array)

Computes a hash code for a multi-dimensional array.

public static int ComputeHash(this Array array)

Parameters

array Array

The array for which to compute the hash code.

Returns

int

The computed hash code.

ComputeHash(IEnumerable<object>)

Computes a hash code based on the hash codes of the given objects.

public static int ComputeHash(this IEnumerable<object> objects)

Parameters

objects IEnumerable<object>

The objects to include in the hash computation.

Returns

int

The computed hash code.

ComputeHash(params object[])

Computes a hash code based on the hash codes of the given objects.

public static int ComputeHash(params object[] objects)

Parameters

objects object[]

The objects to include in the hash computation.

Returns

int

The computed hash code.

ComputeHash<T>(Array, Func<T, int>)

Computes a hash code for a multi-dimensional array using a custom hash code function.

public static int ComputeHash<T>(this Array array, Func<T, int> getHashCode)

Parameters

array Array

The array for which to compute the hash code.

getHashCode Func<T, int>

The custom hash code function.

Returns

int

The computed hash code.

Type Parameters

T

The type of elements in the array.

ComputeHash<T>(IEnumerable<T>, Func<T, int>)

Computes a hash code based on a custom hash function and the given objects.

public static int ComputeHash<T>(this IEnumerable<T> objects, Func<T, int> getHashCode)

Parameters

objects IEnumerable<T>

The objects to include in the hash computation.

getHashCode Func<T, int>

The custom hash function.

Returns

int

The computed hash code.

Type Parameters

T

The type of objects to hash.

ComputeHash<T>(Func<T, int>, params IEnumerable<T>)

Computes a hash code based on a custom hash function and the given objects.

public static int ComputeHash<T>(Func<T, int> getHashCode, params IEnumerable<T> objects)

Parameters

getHashCode Func<T, int>

The custom hash function.

objects IEnumerable<T>

The objects to include in the hash computation.

Returns

int

The computed hash code.

Type Parameters

T

The type of objects to hash.

ComputeHash<T>(ReadOnlySpan<T>)

Computes a hash code based on a custom hash function and the given objects.

public static int ComputeHash<T>(this ReadOnlySpan<T> objects)

Parameters

objects ReadOnlySpan<T>

The objects to include in the hash computation.

Returns

int

The computed hash code.

Type Parameters

T

The type of objects to hash.

ComputeHash<T>(ReadOnlySpan<T>, Func<T, int>)

Computes a hash code based on a custom hash function and the given objects.

public static int ComputeHash<T>(this ReadOnlySpan<T> objects, Func<T, int> getHashCode)

Parameters

objects ReadOnlySpan<T>

The objects to include in the hash computation.

getHashCode Func<T, int>

The custom hash function.

Returns

int

The computed hash code.

Type Parameters

T

The type of objects to hash.

DoAsync<T, Result>(T, Func<T, Task<Result>>, Func<Task<Result>>)

Asynchronously executes the specified async function if the object is not null; otherwise, executes the async fallback function.

public static Task<Result> DoAsync<T, Result>(this T value, Func<T, Task<Result>> ifNotNull, Func<Task<Result>> ifNull)

Parameters

value T

The object to check.

ifNotNull Func<T, Task<Result>>

The asynchronous function to execute if the object is not null.

ifNull Func<Task<Result>>

The asynchronous function to execute if the object is null.

Returns

Task<Result>

A task representing the asynchronous operation.

Type Parameters

T

The type of the object.

Result

The type of the result.

Remarks

Unlike the synchronous-delegate overloads, this composes the tasks returned by ifNotNull and ifNull directly, without offloading to the thread pool via Task.Run(Func<Result>).

DoAsync<T, Result>(T, Func<T, Task<Result>>, Result)

Asynchronously executes the specified async function if the object is not null; otherwise, returns a fallback value.

public static Task<Result> DoAsync<T, Result>(this T value, Func<T, Task<Result>> ifNotNull, Result ifNull)

Parameters

value T

The object to check.

ifNotNull Func<T, Task<Result>>

The asynchronous function to execute if the object is not null.

ifNull Result

The value to return if the object is null.

Returns

Task<Result>

A task representing the asynchronous operation.

Type Parameters

T

The type of the object.

Result

The type of the result.

Remarks

Unlike the synchronous-delegate overload, this composes the task returned by ifNotNull directly, without offloading to the thread pool via Task.Run(Func<Result>).

DoAsync<T, Result>(T, Func<T, Result>, Func<Result>)

Executes the specified synchronous function on the thread pool if the object is not null; otherwise, executes the synchronous fallback function.

public static Task<Result> DoAsync<T, Result>(this T value, Func<T, Result> ifNotNull, Func<Result> ifNull)

Parameters

value T

The object to check.

ifNotNull Func<T, Result>

The synchronous function to execute if the object is not null.

ifNull Func<Result>

The synchronous function to execute if the object is null.

Returns

Task<Result>

A task representing the asynchronous operation.

Type Parameters

T

The type of the object.

Result

The type of the result.

Remarks

ifNotNull and ifNull are synchronous delegates offloaded to the thread pool via Task.Run(Func<Result>); this consumes a thread-pool thread for the duration of the call. For delegates that are already asynchronous, use the DoAsync<T, Result>(T, Func<T, Task<Result>>, Func<Task<Result>>) overload instead, which composes the returned tasks directly without occupying an extra thread.

DoAsync<T, Result>(T, Func<T, Result>, Result)

Executes the specified synchronous function on the thread pool if the object is not null; otherwise, returns a fallback value.

public static Task<Result> DoAsync<T, Result>(this T value, Func<T, Result> ifNotNull, Result ifNull)

Parameters

value T

The object to check.

ifNotNull Func<T, Result>

The synchronous function to execute if the object is not null.

ifNull Result

The value to return if the object is null.

Returns

Task<Result>

A task representing the asynchronous operation.

Type Parameters

T

The type of the object.

Result

The type of the result.

Remarks

ifNotNull is a synchronous delegate offloaded to the thread pool via Task.Run(Func<Result>); this consumes a thread-pool thread for the duration of the call. For a delegate that is already asynchronous, use the DoAsync<T, Result>(T, Func<T, Task<Result>>, Result) overload instead, which composes the returned task directly without occupying an extra thread.

Do<T, Result>(T, Func<T, Result>, Func<Result>)

Executes the specified function if the object is not null; otherwise, executes the fallback function.

public static Result Do<T, Result>(this T value, Func<T, Result> ifNotNull, Func<Result> ifNull)

Parameters

value T

The object to check.

ifNotNull Func<T, Result>

The function to execute if the object is not null.

ifNull Func<Result>

The function to execute if the object is null.

Returns

Result

The result of the executed function.

Type Parameters

T

The type of the object.

Result

The type of the result.

Do<T, Result>(T, Func<T, Result>, Result)

Executes the specified function if the object is not null; otherwise, returns a fallback value.

public static Result Do<T, Result>(this T value, Func<T, Result> ifNotNull, Result ifNull)

Parameters

value T

The object to check.

ifNotNull Func<T, Result>

The function to execute if the object is not null.

ifNull Result

The value to return if the object is null.

Returns

Result

The result of the executed function or the fallback value.

Type Parameters

T

The type of the object.

Result

The type of the result.

In<T>(T, params T[])

Determines whether a value is contained in the specified array.

public static bool In<T>(this T value, params T[] objects)

Parameters

value T

The value to check.

objects T[]

The array of values to check against.

Returns

bool

True if the value is found in the array; otherwise, false.

Type Parameters

T

The type of the value.

IndexOf<T>(IEnumerable<T>, Func<T, bool>)

Returns the index of the first element in the enumerable that satisfies the specified condition, or -1 if not found.

public static int IndexOf<T>(this IEnumerable<T> enumeration, Func<T, bool> predicate)

Parameters

enumeration IEnumerable<T>

The enumerable to search.

predicate Func<T, bool>

The condition to satisfy.

Returns

int

The index of the first matching element if found; otherwise, -1.

Type Parameters

T

The type of elements in the enumerable.

IndexOf<T>(IEnumerable<T>, T)

Returns the index of the specified value in the enumerable, or -1 if not found.

public static int IndexOf<T>(this IEnumerable<T> enumeration, T toFind)

Parameters

enumeration IEnumerable<T>

The enumerable to search.

toFind T

The value to find.

Returns

int

The index of the value if found; otherwise, -1.

Type Parameters

T

The type of elements in the enumerable.

IndexOf<T>(IEnumerable<T>, T, IEqualityComparer<T>)

Returns the index of the specified value in the enumerable, using a custom comparer, or -1 if not found.

public static int IndexOf<T>(this IEnumerable<T> enumeration, T toFind, IEqualityComparer<T> comparer)

Parameters

enumeration IEnumerable<T>

The enumerable to search.

toFind T

The value to find.

comparer IEqualityComparer<T>

The custom comparer to use.

Returns

int

The index of the value if found; otherwise, -1.

Type Parameters

T

The type of elements in the enumerable.

IsNullOrDefault<T>(T?)

Returns true if the given nullable object is either null or equal to the default value of its type.

public static bool IsNullOrDefault<T>(this T? nullableObj) where T : struct

Parameters

nullableObj T?

The nullable object to check.

Returns

bool

True if the object is null or its default value; otherwise, false.

Type Parameters

T

The underlying value type.

NotIn<T>(T, params T[])

Determines whether a value is not contained in the specified array.

public static bool NotIn<T>(this T value, params T[] objects)

Parameters

value T

The value to check.

objects T[]

The array of values to check against.

Returns

bool

True if the value is not found in the array; otherwise, false.

Type Parameters

T

The type of the value.

Swap<T>(ref T, ref T)

Swaps the values of two objects.

public static void Swap<T>(ref T obj1, ref T obj2)

Parameters

obj1 T

The first object.

obj2 T

The second object.

Type Parameters

T

The type of the objects.