ComparableHelper Class
Definition
Provides null-tolerant ordering helpers - Min, Max, and Coalesce - for pairs of operands.
Min and Max require IComparable<T> operands (reference types may be
null); Coalesce is unconstrained and additionally accepts Nullable<T> value
types.
public static class ComparableHelper
- Inheritance
-
ComparableHelper
- Inherited Members
Examples
string? a = "alpha";
string? b = null;
string? larger = ComparableHelper.Max(a, b); // "alpha"
string? smaller = ComparableHelper.Min(a, b); // "alpha" (null is skipped)
string? either = ComparableHelper.Coalesce(b, a); // "alpha"
// Coalesce alone is unconstrained, so it also accepts nullable value types.
int? first = ComparableHelper.Coalesce((int?)null, 5); // 5
// Custom comparer for case-insensitive string ordering.
string? winner = ComparableHelper.Max("alpha", "Beta", StringComparer.OrdinalIgnoreCase); // "Beta"
Remarks
The helpers are designed for the common case in which one or both operands may be null and the
caller wants the non-null value to win rather than to throw. Coalesce returns the first non-null operand and
is equivalent to the null-coalescing operator over reference and nullable value types. Min and Max
return the smaller or larger operand respectively; when one operand is null the other is returned
unchanged, and when both are null the result is null.
Each method accepts an optional IComparer<T> override; when omitted, Default is used. Because the ordering call only occurs when both operands are non-null, callers can supply comparers that throw on null input without guarding separately.
These helpers complement ComparableExtensions, which provides instance-style comparison predicates (
IsBetween, IsGreaterThan, and so on). ComparableHelper is the static surface for
combining two operands; ComparableExtensions is the surface for testing a single operand against
bounds.
Methods
Coalesce<T>(T?, T?)
Returns the first non-null value from two specified values.
public static T? Coalesce<T>(T? first, T? second)
Parameters
firstTThe first value to return if not
null.secondTThe second value to return if the first is null.
Returns
- T
The first non-null value, or
nullif both values arenull.
Type Parameters
TThe type of the values.
Max<T>(T?, T?)
Returns the larger of two specified values.
public static T? Max<T>(T? first, T? second) where T : IComparable<T>
Parameters
firstTThe first value to compare.
secondTThe second value to compare.
Returns
- T
The larger of the two values. If one value is null, the non-null value is returned. If both values are
null,nullis returned.
Type Parameters
TThe type of the values to compare, which must implement IComparable<T>.
Remarks
This method compares the two values using the CompareTo(T) method.
Max<T>(T?, T?, IComparer<T>)
Returns the larger of two specified values using a custom IComparer<T>.
public static T? Max<T>(T? first, T? second, IComparer<T> comparer)
Parameters
firstTThe first value to compare.
secondTThe second value to compare.
comparerIComparer<T>The comparer to use for comparing the values.
Returns
- T
The larger of the two values based on the specified comparer. If one value is null, the non-null value is returned. If both values are
null,nullis returned.
Type Parameters
TThe type of the values to compare.
Remarks
Use this overload to apply custom comparison logic, such as ordinal or case-insensitive string comparisons.
Exceptions
- ArgumentNullException
Thrown when
compareris null.
Min<T>(T?, T?)
Returns the smaller of two specified values.
public static T? Min<T>(T? first, T? second) where T : IComparable<T>
Parameters
firstTThe first value to compare.
secondTThe second value to compare.
Returns
- T
The smaller of the two values. If one value is null, the non-null value is returned. If both values are
null,nullis returned.
Type Parameters
TThe type of the values to compare, which must implement IComparable<T>.
Remarks
This method compares the two values using the CompareTo(T) method.
Min<T>(T?, T?, IComparer<T>)
Returns the smaller of two specified values using a custom IComparer<T>.
public static T? Min<T>(T? first, T? second, IComparer<T> comparer)
Parameters
firstTThe first value to compare.
secondTThe second value to compare.
comparerIComparer<T>The comparer to use for comparing the values.
Returns
- T
The smaller of the two values based on the specified comparer. If one value is null, the non-null value is returned. If both values are
null,nullis returned.
Type Parameters
TThe type of the values to compare.
Remarks
Use this overload to apply custom comparison logic, such as culture-specific string comparisons.
Exceptions
- ArgumentNullException
Thrown when
compareris null.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |