Description
The Clamp
method is a generic extension method that constrains a value to lie within a specified range. It ensures that the input value does not exceed the specified minimum and maximum bounds. If the input value is less than the minimum, the method returns the minimum. If the input value is greater than the maximum, the method returns the maximum. Otherwise, it returns the input value itself.
Usage
To use the Clamp
method, you need to provide three parameters:
input
: The value to be clamped.
min
: The minimum allowable value.
max
: The maximum allowable value.
The method returns the clamped value, ensuring it is within the specified range.
Example
// Example usage of the Clamp method
int clampedValue = SandboxSystemExtensions.Clamp(15, 10, 20);
// clampedValue will be 15, as it is within the range 10 to 20
int clampedValueBelow = SandboxSystemExtensions.Clamp(5, 10, 20);
// clampedValueBelow will be 10, as 5 is below the minimum range
int clampedValueAbove = SandboxSystemExtensions.Clamp(25, 10, 20);
// clampedValueAbove will be 20, as 25 is above the maximum range