Class ObjectUtils
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
valueTThe value to check.
comparerIComparer<T>The custom comparer to use.
lowerBoundTThe lower bound.
upperBoundTThe upper bound.
Returns
- bool
True if the value is between the bounds; otherwise, false.
Type Parameters
TThe 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
valueTThe value to check.
comparerIComparer<T>The custom comparer to use.
lowerBoundTThe lower bound.
upperBoundTThe upper bound.
includeLowerBoundboolWhether to include the lower bound in the comparison.
includeUpperBoundboolWhether 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
TThe 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
valueTThe value to check.
lowerBoundTThe lower bound.
upperBoundTThe upper bound.
Returns
- bool
True if the value is between the bounds; otherwise, false.
Type Parameters
TThe 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
valueTThe value to check.
lowerBoundTThe lower bound.
upperBoundTThe upper bound.
includeLowerBoundboolWhether to include the lower bound in the comparison.
includeUpperBoundboolWhether 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
TThe type of the value.
ComputeHash(Array)
Computes a hash code for a multi-dimensional array.
public static int ComputeHash(this Array array)
Parameters
arrayArrayThe 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
objectsIEnumerable<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
objectsobject[]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
arrayArrayThe array for which to compute the hash code.
getHashCodeFunc<T, int>The custom hash code function.
Returns
- int
The computed hash code.
Type Parameters
TThe 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
objectsIEnumerable<T>The objects to include in the hash computation.
getHashCodeFunc<T, int>The custom hash function.
Returns
- int
The computed hash code.
Type Parameters
TThe 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
getHashCodeFunc<T, int>The custom hash function.
objectsIEnumerable<T>The objects to include in the hash computation.
Returns
- int
The computed hash code.
Type Parameters
TThe 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
objectsReadOnlySpan<T>The objects to include in the hash computation.
Returns
- int
The computed hash code.
Type Parameters
TThe 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
objectsReadOnlySpan<T>The objects to include in the hash computation.
getHashCodeFunc<T, int>The custom hash function.
Returns
- int
The computed hash code.
Type Parameters
TThe 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
valueTThe object to check.
ifNotNullFunc<T, Task<Result>>The asynchronous function to execute if the object is not null.
ifNullFunc<Task<Result>>The asynchronous function to execute if the object is null.
Returns
- Task<Result>
A task representing the asynchronous operation.
Type Parameters
TThe type of the object.
ResultThe 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
valueTThe object to check.
ifNotNullFunc<T, Task<Result>>The asynchronous function to execute if the object is not null.
ifNullResultThe value to return if the object is null.
Returns
- Task<Result>
A task representing the asynchronous operation.
Type Parameters
TThe type of the object.
ResultThe 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
valueTThe object to check.
ifNotNullFunc<T, Result>The synchronous function to execute if the object is not null.
ifNullFunc<Result>The synchronous function to execute if the object is null.
Returns
- Task<Result>
A task representing the asynchronous operation.
Type Parameters
TThe type of the object.
ResultThe 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
valueTThe object to check.
ifNotNullFunc<T, Result>The synchronous function to execute if the object is not null.
ifNullResultThe value to return if the object is null.
Returns
- Task<Result>
A task representing the asynchronous operation.
Type Parameters
TThe type of the object.
ResultThe 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
valueTThe object to check.
ifNotNullFunc<T, Result>The function to execute if the object is not null.
ifNullFunc<Result>The function to execute if the object is null.
Returns
- Result
The result of the executed function.
Type Parameters
TThe type of the object.
ResultThe 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
valueTThe object to check.
ifNotNullFunc<T, Result>The function to execute if the object is not null.
ifNullResultThe value to return if the object is null.
Returns
- Result
The result of the executed function or the fallback value.
Type Parameters
TThe type of the object.
ResultThe 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
valueTThe value to check.
objectsT[]The array of values to check against.
Returns
- bool
True if the value is found in the array; otherwise, false.
Type Parameters
TThe 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
enumerationIEnumerable<T>The enumerable to search.
predicateFunc<T, bool>The condition to satisfy.
Returns
- int
The index of the first matching element if found; otherwise, -1.
Type Parameters
TThe 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
enumerationIEnumerable<T>The enumerable to search.
toFindTThe value to find.
Returns
- int
The index of the value if found; otherwise, -1.
Type Parameters
TThe 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
enumerationIEnumerable<T>The enumerable to search.
toFindTThe value to find.
comparerIEqualityComparer<T>The custom comparer to use.
Returns
- int
The index of the value if found; otherwise, -1.
Type Parameters
TThe 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
nullableObjT?The nullable object to check.
Returns
- bool
True if the object is null or its default value; otherwise, false.
Type Parameters
TThe 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
valueTThe value to check.
objectsT[]The array of values to check against.
Returns
- bool
True if the value is not found in the array; otherwise, false.
Type Parameters
TThe 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
obj1TThe first object.
obj2TThe second object.
Type Parameters
TThe type of the objects.