Skip to content

IComparable Extensions

Andrzej Kebab edited this page Jan 21, 2024 · 2 revisions

UtilityLibrary.Core

This utility library provides extension methods for types implementing IComparable<T> to simplify common comparison operations.

Between

Between

Determines whether the specified value is between the lower and upper bounds.

using UtilityLibrary.Core;

int value = 5;
int lowerBound = 2;
int upperBound = 8;
bool isBetween = value.Between(lowerBound, upperBound, includeLowerBound: true, includeUpperBound: true);

Parameters

  • value (T): The value to compare.
  • lowerBound (T): The lower bound.
  • upperBound (T): The upper bound.
  • includeLowerBound (bool): If set to true, includes the lower bound in the comparison.
  • includeUpperBound (bool): If set to true, includes the upper bound in the comparison.

Returns

  • bool: True if the specified value is between the bounds; otherwise, False.

Clamp

Clamp

Clamps the specified value within the min and max range.

using UtilityLibrary.Core;

int value = 15;
int min = 5;
int max = 10;
int clampedValue = value.Clamp(min, max);

Parameters

  • value (T): The value to clamp.
  • min (T): The minimum value.
  • max (T): The maximum value.

Returns

  • T: The clamped value.

Comparison

LessThan

Determines whether the specified value is less than the other specified value.

using UtilityLibrary.Core;

int value = 5;
int other = 10;
bool isLessThan = value.LessThan(other);

Parameters

  • value (T): The value to compare.
  • other (T): The other value to compare.

Returns

  • bool: True if the specified value is less than the other value; otherwise, False.

LessOrEqual

Determines whether the specified value is less than or equal to the other specified value.

using UtilityLibrary.Core;

int value = 5;
int other = 5;
bool isLessOrEqual = value.LessOrEqual(other);

Parameters

  • value (T): The value to compare.
  • other (T): The other value to compare.

Returns

  • bool: True if the specified value is less than or equal to the other value; otherwise, False.

GreaterThan

Determines whether the specified value is greater than the other specified value.

using UtilityLibrary.Core;

int value = 10;
int other = 5;
bool isGreaterThan = value.GreaterThan(other);

Parameters

  • value (T): The value to compare.
  • other (T): The other value to compare.

Returns

  • bool: True if the specified value is greater than the other value; otherwise, False.

GreaterOrEqual

Determines whether the specified value is greater than or equal to the other specified value.

using UtilityLibrary.Core;

int value = 10;
int other = 10;
bool isGreaterOrEqual = value.GreaterOrEqual(other);

Parameters

  • value (T): The value to compare.
  • other (T): The other value to compare.

Returns

  • bool: True if the specified value is greater than or equal to the other value; otherwise, False.

Clone this wiki locally